<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>Ruby 备忘清单
 &#x26;  ruby cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="Ruby 备忘清单
===

入门，为开发人员分享快速参考备忘单。">
<meta keywords="ruby,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="..\style\style.css">
<link rel="stylesheet" href="..\style\katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="..\index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/ruby.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.4.1"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="ruby-备忘清单"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" fill="currentColor" height="1em" width="1em">
  <path d="M26.875.109c4.043.703 5.193 3.464 5.104 6.36L32 6.426l-1.817 23.855-23.641 1.62c-1.963-.084-6.344-.261-6.541-6.38l2.193-4 3.76 8.781.672 1.561 3.739-12.192-.041.011.021-.043 12.339 3.944-1.86-7.245-1.317-5.199 11.755-.76-.817-.677-8.443-6.885 4.88-2.719zM0 25.453v.031-.036zM6.839 6.765c4.749-4.713 10.875-7.495 13.229-5.12 2.348 2.371-.141 8.141-4.896 12.844-4.751 4.713-10.803 7.647-13.151 5.276-2.355-2.364.057-8.287 4.817-13z"></path>
</svg>
<a aria-hidden="true" tabindex="-1" href="#ruby-备忘清单"><span class="icon icon-link"></span></a>Ruby 备忘清单</h1><div class="wrap-body">
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#安装">安装</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#什么是-gemfile-和-gemfilelock">什么是 Gemfile 和 Gemfile.lock</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#安装特定-ruby-gem-的特定版本">安装特定 ruby gem 的特定版本</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用-bundler-更新-gem">使用 Bundler 更新 gem</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#保留字">保留字</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#注释">注释</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#运算符">运算符</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#逻辑运算符">逻辑运算符</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#位运算符">位运算符</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#算术运算符">算术运算符</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#比较运算符">比较运算符</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#运算符示例">运算符示例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#运算符优先级表">运算符优先级表</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变量和范围">变量和范围</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#局部变量">局部变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#实例变量">实例变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#伪变量">伪变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#选项变量">选项变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#预定义变量">预定义变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#预定义的全局常量">预定义的全局常量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#检查变量的范围">检查变量的范围</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数据类型">数据类型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#检查数据类型">检查数据类型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#symbol">Symbol</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#整数有用的方法">整数有用的方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#范围">范围</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#在-range-中使用-step">在 Range 中使用 step</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#条件结构">条件结构</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#if-修饰符">if 修饰符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#if-elsif-else-语句">if elsif else 语句</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#除非语句">除非语句</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#case-陈述">case 陈述</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#case-简短的语法">case 简短的语法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#case-可选的失败">case 可选的失败</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#case-获取返回值">case 获取返回值</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#字符串">字符串</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串插值">字符串插值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#提取子字符串">提取子字符串</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#将字符串转换为小写或大写">将字符串转换为小写或大写</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#有用的方法">有用的方法</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#方法">方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#声明一个方法">声明一个方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#调用方法">调用方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#类方法">类方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用另一个参数作为默认值">使用另一个参数作为默认值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#为方法参数定义默认值">为方法参数定义默认值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#将可变长度参数传递给方法参数">将可变长度参数传递给方法参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#修改对象">修改对象</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#布尔方法">布尔方法</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#blocks-块">Blocks (块)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#块示例">块示例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#单行块">单行块</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#多行块">多行块</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#隐式传递一个块">隐式传递一个块</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#多次调用">多次调用</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用块参数调用">使用块参数调用</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用多个块参数调用">使用多个块参数调用</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#块将尝试从当前上下文返回">块将尝试从当前上下文返回</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#通过使用--参数显式传递块">通过使用 &#x26; 参数显式传递块</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#检查是否给出了块">检查是否给出了块</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#处理异常并使块可选的方法">处理异常并使块可选的方法</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#procs">Procs</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#procs-示例">Procs 示例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#任意参数">任意参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#proc-将尝试从当前上下文返回">proc 将尝试从当前上下文返回</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#不能从顶级上下文返回">不能从顶级上下文返回</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#lambdas">Lambdas</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#声明一个-lambda">声明一个 lambda</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#严格的-arguments">严格的 arguments</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#块中声明一个-lambda">块中声明一个 lambda</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#lambdas-从-lambda-本身返回就像常规方法一样">lambdas 从 lambda 本身返回，就像常规方法一样</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#数组">数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#初始化一个空数组">初始化一个空数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#包含不同类型的对象的数组">包含不同类型的对象的数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#用初始大小和默认对象填充数组">用初始大小和默认对象填充数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#不同哈希值的数组">不同哈希值的数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#二维数组">二维数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数组索引">数组索引</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#负索引">负索引</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数组方法-at">数组方法 at</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#范围获取">范围获取</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数组方法-fetch">数组方法 fetch</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#获取数组元素">获取数组元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#在数组末尾添加值-push">在数组末尾添加值 push</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数组末尾删除值-pop">数组末尾删除值 pop</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#在数组的开头添加值-unshift">在数组的开头添加值 unshift</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#检索并同时删除第一个元素-shift">检索并同时删除第一个元素 shift</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除特定索引处的元素-delete_at">删除特定索引处的元素 delete_at</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除数组中任意位置的特定元素">删除数组中任意位置的特定元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#在给定索引处插入值-insert">在给定索引处插入值 insert</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#一个块来填充数组的值">一个块来填充数组的值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#填充数组变得更容易">填充数组变得更容易</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#从数组中删除-nil-值">从数组中删除 nil 值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#去重-uniq">去重 uniq</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#检查数组中是否存在值include">检查数组中是否存在值（include？）</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#获取数组大小">获取数组大小</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#清除数组">清除数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#获取数组的第一个元素">获取数组的第一个元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#获取数组的最后一个元素">获取数组的最后一个元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#合并两个数组">合并两个数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#对数组进行排序">对数组进行排序</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#从数组中获取最大值">从数组中获取最大值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用范围获取数组元素">使用范围获取数组元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#获取数组的前n个元素">获取数组的前n个元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#访问元素">访问元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#从数组中删除重复元素">从数组中删除重复元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除前-n-个元素">删除前 n 个元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除第一个元素">删除第一个元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除最后一个元素">删除最后一个元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除带有索引的元素">删除带有索引的元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除所有出现的元素">删除所有出现的元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#each">each</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#each_with_index">each_with_index</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#each_index">each_index</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#map">map</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#collect">collect</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#for">for</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#each_with_object">each_with_object</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#while">while</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#do-while">do while</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#until">until</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#times">times</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#upto">upto</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#downto">downto</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#step">step</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#inject">inject</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#reduce">reduce</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#detect">detect</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#find">find</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#select">select</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#reject">reject</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#keep_if">keep_if</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#delete_if">delete_if</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#drop_while">drop_while</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#reverse_each">reverse_each</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#布尔可枚举方法">布尔可枚举方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#布尔可枚举方法-1">布尔可枚举方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#all">all?</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#any">any?</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#one">one?</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#none">none?</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#empty">empty?</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#组合方法">组合方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#组合方法-1">组合方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#">&#x26;</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#intersection">intersection</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#-1">+</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#-">-</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#union">union</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#difference">difference</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#product">product</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#循环">循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#while-循环">while 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#for-循环">for 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#dowhile-循环">do..while 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#until-循环">until 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#跳出循环">跳出循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#在循环内跳过">在循环内跳过</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#重复当前迭代">重复当前迭代</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#重新开始循环">重新开始循环</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#classes">Classes</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#classes-示例">Classes 示例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#继承一个类">继承一个类</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#检查实例类型">检查实例类型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#打印一个类的所有方法名">打印一个类的所有方法名</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#检查一个类是否有特定的方法">检查一个类是否有特定的方法</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-4"><div class="wrap-header h3wrap"><h3 id="安装"><a aria-hidden="true" tabindex="-1" href="#安装"><span class="icon icon-link"></span></a>安装</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-4-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># Debian, Ubuntu</span>
</span><span class="code-line">$ <span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> ruby-full
</span><span class="code-line"><span class="token comment"># Windows</span>
</span><span class="code-line">$ winget <span class="token function">install</span> RubyInstallerTeam.Ruby 
</span><span class="code-line">$ brew <span class="token function">install</span> ruby <span class="token comment"># macOS</span>
</span><span class="code-line">$ <span class="token function">docker</span> run <span class="token parameter variable">-it</span> <span class="token parameter variable">--rm</span> ruby:latest <span class="token comment"># Docker</span>
</span><span class="code-line">$ <span class="token function">docker</span> run <span class="token parameter variable">-it</span> <span class="token parameter variable">--rm</span> ruby:2.7
</span></code></pre>
<p>使用包管理器安装 <a href="https://github.com/rbenv/rbenv#readme">rbenv</a></p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ brew <span class="token function">install</span> rbenv ruby-build <span class="token comment"># macOS</span>
</span><span class="code-line"><span class="token comment"># Debian、ubuntu 和其他衍生产品</span>
</span><span class="code-line">$ <span class="token function">sudo</span> <span class="token function">apt</span> <span class="token function">install</span> rbenv
</span></code></pre>
<p>使用 <a href="https://github.com/rbenv/rbenv#readme">rbenv</a> 安装 ruby</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 列出最新的稳定版本</span>
</span><span class="code-line">$ rbenv <span class="token function">install</span> <span class="token parameter variable">-l</span>
</span><span class="code-line"><span class="token comment"># 列出所有本地版本</span>
</span><span class="code-line">$ rbenv <span class="token function">install</span> <span class="token parameter variable">-L</span>
</span><span class="code-line"><span class="token comment"># 安装 Ruby 版本</span>
</span><span class="code-line">$ rbenv <span class="token function">install</span> <span class="token number">3.1</span>.2
</span><span class="code-line">$ rbenv global <span class="token number">3.1</span>.2 <span class="token comment"># 为这台机器设置默认Ruby版本</span>
</span><span class="code-line"><span class="token comment"># 或者</span>
</span><span class="code-line">$ rbenv <span class="token builtin class-name">local</span> <span class="token number">3.1</span>.2 <span class="token comment"># 设置此目录的 Ruby 版本</span>
</span><span class="code-line">$ rbenv <span class="token builtin class-name">local</span> <span class="token parameter variable">--unset</span>
</span><span class="code-line">$ rbenv version <span class="token comment"># 显示当前活动的 Ruby 版本</span>
</span><span class="code-line"><span class="token operator">=</span><span class="token operator">></span> <span class="token number">1.9</span>.3-p327 <span class="token punctuation">(</span>set by /Users/sam/.rbenv/version<span class="token punctuation">)</span>
</span></code></pre>
<p>使用 <a href="https://www.ruby-lang.org/en/documentation/installation/#rvm">RVM</a> 安装 ruby</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">curl</span> <span class="token parameter variable">-sSL</span> https://get.rvm.io <span class="token operator">|</span> <span class="token function">bash</span> <span class="token parameter variable">-s</span> stable
</span><span class="code-line">$ rvm list          <span class="token comment"># Ruby 版本列表</span>
</span><span class="code-line">$ rvm <span class="token function">install</span> <span class="token number">3.0</span>.1 <span class="token comment"># 安装 3.0.1</span>
</span><span class="code-line">$ rvm use <span class="token number">3.0</span>.1     <span class="token comment"># 使用 3.0.1</span>
</span></code></pre>
<p>如何安装 ruby gem 管理器, <a href="https://bundler.io/">bundler</a> gem</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 访问 bash 以执行以下命令</span>
</span><span class="code-line">$ <span class="token function">docker</span> run <span class="token parameter variable">-it</span> <span class="token parameter variable">--rm</span> ruby:latest <span class="token function">bash</span>
</span><span class="code-line">$ gem <span class="token function">install</span> bundler
</span><span class="code-line">$ bundle <span class="token parameter variable">-v</span>
</span><span class="code-line">$ gem update bundler
</span><span class="code-line">$ gem uninstall bundler
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="什么是-gemfile-和-gemfilelock"><a aria-hidden="true" tabindex="-1" href="#什么是-gemfile-和-gemfilelock"><span class="icon icon-link"></span></a>什么是 Gemfile 和 Gemfile.lock</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p><a href="https://bundler.io/v2.3/man/gemfile.5.html">Gemfile</a> 是 Bundler(也是 gem)的配置文件，其中包含项目的 gem 列表(依赖项)</p>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token comment"># 在项目根目录的 Gemfile 中指定 gem</span>
</span><span class="code-line">ruby <span class="token string-literal"><span class="token string">'2.5.6'</span></span>
</span><span class="code-line">
</span><span class="code-line">source <span class="token string-literal"><span class="token string">'https://rubygems.org'</span></span>
</span><span class="code-line">gem <span class="token string-literal"><span class="token string">'nokogiri'</span></span>
</span><span class="code-line">gem <span class="token string-literal"><span class="token string">'rack'</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">'~>1.1'</span></span>
</span><span class="code-line">gem <span class="token string-literal"><span class="token string">'rspec'</span></span><span class="token punctuation">,</span> <span class="token symbol">:require</span> <span class="token operator">=></span> <span class="token string-literal"><span class="token string">'spec'</span></span>
</span></code></pre>
<p>安装 Gemfile 中的所有 gem</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ bundle <span class="token function">install</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="安装特定-ruby-gem-的特定版本"><a aria-hidden="true" tabindex="-1" href="#安装特定-ruby-gem-的特定版本"><span class="icon icon-link"></span></a>安装特定 ruby gem 的特定版本</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ gem <span class="token function">install</span> bundler <span class="token parameter variable">-v</span> <span class="token number">1.17</span>
</span><span class="code-line">$ gem <span class="token function">install</span> minitest <span class="token parameter variable">-v</span> <span class="token number">5.8</span>.4
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用-bundler-更新-gem"><a aria-hidden="true" tabindex="-1" href="#使用-bundler-更新-gem"><span class="icon icon-link"></span></a>使用 Bundler 更新 gem</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 使用 Bundler 更新单个 gem</span>
</span><span class="code-line">$ bundle update nokogiri
</span><span class="code-line"><span class="token comment"># 使用 Bundler 更新 Gemfile 中的每个 gem</span>
</span><span class="code-line">$ bundle update
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-5"><div class="wrap-header h3wrap"><h3 id="保留字"><a aria-hidden="true" tabindex="-1" href="#保留字"><span class="icon icon-link"></span></a>保留字</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-5-->









































































































































































<table><thead><tr><th align="left">保留字</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>__ENCODING__</code></td><td>当前文件的脚本编码</td></tr><tr><td align="left"><code>__LINE__</code></td><td>当前文件中此关键字的行号</td></tr><tr><td align="left"><code>__FILE__</code></td><td>当前文件的路径</td></tr><tr><td align="left"><code>BEGIN</code></td><td>包含在 <code>{ }</code> 中的代码在程序运行之前运行</td></tr><tr><td align="left"><code>END</code></td><td>包含在 <code>{ }</code> 中以在程序结束时运行</td></tr><tr><td align="left"><code>alias</code></td><td>为现有方法、运算符、全局变量创建别名</td></tr><tr><td align="left"><code>and</code></td><td>逻辑与运算符</td></tr><tr><td align="left"><code>begin</code></td><td>开始一段代码</td></tr><tr><td align="left"><code>break</code></td><td>终止循环</td></tr><tr><td align="left"><code>case</code></td><td>将表达式与匹配的 <code>when</code> 子句进行比较，其中 <br> 以 <code>end</code> 结束</td></tr><tr><td align="left"><code>class</code></td><td>定义一个类</td></tr><tr><td align="left"><code>def</code></td><td>定义函数/方法</td></tr><tr><td align="left"><code>defined?</code></td><td>检查某个变量、函数是否存在</td></tr><tr><td align="left"><code>do</code></td><td>开始一个代码块并执行块中的代码，以<br> <strong>end关键字</strong>结束</td></tr><tr><td align="left"><code>else</code></td><td>如果先前的条件不成立，则执行以下代码</td></tr><tr><td align="left"><code>elsif</code></td><td>if 表达式的替代条件</td></tr><tr><td align="left"><code>end</code></td><td>用于结束以 <code>begin</code>、<code>class</code>、<code>def</code>、<code>do</code>、<code>if</code> 等关键字开头的代码块</td></tr><tr><td align="left"><code>ensure</code></td><td>总是在块终止时执行</td></tr><tr><td align="left"><code>false</code></td><td>逻辑布尔假值</td></tr><tr><td align="left"><code>for</code></td><td>开始一个 <code>for</code> 循环</td></tr><tr><td align="left"><code>if</code></td><td>如果 <code>if</code> 的条件语句为 <code>true</code>，则执行代码块</td></tr><tr><td align="left"><code>in</code></td><td>与 <code>for</code> 循环一起使用</td></tr><tr><td align="left"><code>module</code></td><td>定义一个模块</td></tr><tr><td align="left"><code>next</code></td><td>跳转到循环条件评估之前的点</td></tr><tr><td align="left"><code>nil</code></td><td>为空或无效或始终为假</td></tr><tr><td align="left"><code>not</code></td><td>逻辑否定运算符</td></tr><tr><td align="left"><code>or</code></td><td>逻辑或运算符</td></tr><tr><td align="left"><code>redo</code></td><td>条件循环后跳转</td></tr><tr><td align="left"><code>rescue</code></td><td>在引发异常后评估表达式</td></tr><tr><td align="left"><code>retry</code></td><td>• 在救援之外调用时，重复方法调用<br>• 在救援内部调用时，跳转到块顶部</td></tr><tr><td align="left"><code>return</code></td><td>从方法或代码块返回值</td></tr><tr><td align="left"><code>self</code></td><td>当前对象</td></tr><tr><td align="left"><code>super</code></td><td>调用超类中的同名方法</td></tr><tr><td align="left"><code>then</code></td><td>与 <code>if</code>、<code>unless</code>、<code>when</code>、<code>case</code>、<code>rescue</code> 一起使用的分隔符</td></tr><tr><td align="left"><code>true</code></td><td>逻辑布尔真</td></tr><tr><td align="left"><code>undef</code></td><td>使当前类中的方法/函数未定义</td></tr><tr><td align="left"><code>until</code></td><td>在条件语句为假时执行代码块</td></tr><tr><td align="left"><code>when</code></td><td>在 case 语句下开始一个子句</td></tr><tr><td align="left"><code>while</code></td><td>执行代码块，直到条件语句变为假</td></tr><tr><td align="left"><code>yield</code></td><td>执行传递给方法的代码块</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="注释"><a aria-hidden="true" tabindex="-1" href="#注释"><span class="icon icon-link"></span></a>注释</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token comment"># 单行注释</span>
</span><span class="code-line"><span class="token comment">=begin
</span></span><span class="code-line"><span class="token comment">多行
</span></span><span class="code-line"><span class="token comment">注释
</span></span><span class="code-line"><span class="token comment">=end</span>
</span><span class="code-line"><span class="token operator">=</span><span class="token keyword">begin</span> 注释第 <span class="token number">1</span> 行 <span class="token operator">=</span><span class="token keyword">end</span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"Hello world!"</span></span>  <span class="token comment"># 代码的内联注释</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist row-span-7"><div class="wrap-header h3wrap"><h3 id="运算符"><a aria-hidden="true" tabindex="-1" href="#运算符"><span class="icon icon-link"></span></a>运算符</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-7-->
<h4 id="逻辑运算符"><a aria-hidden="true" tabindex="-1" href="#逻辑运算符"><span class="icon icon-link"></span></a>逻辑运算符</h4>
<ul class="cols-3 style-none">
<li><code>and</code></li>
<li><code>or</code></li>
<li><code>not</code></li>
<li><code>&#x26;&#x26;</code></li>
<li><code>||</code></li>
<li><code>!</code></li>
</ul>
<!--rehype:className=cols-3 style-none-->
<h4 id="位运算符"><a aria-hidden="true" tabindex="-1" href="#位运算符"><span class="icon icon-link"></span></a>位运算符</h4>
<ul class="cols-3 style-none">
<li><code>&#x26;</code></li>
<li><code>|</code></li>
<li><code>^</code></li>
<li><code>~</code></li>
<li><code>&#x3C;&#x3C;</code></li>
<li><code>>></code></li>
</ul>
<!--rehype:className=cols-3 style-none-->
<h4 id="算术运算符"><a aria-hidden="true" tabindex="-1" href="#算术运算符"><span class="icon icon-link"></span></a>算术运算符</h4>
<ul class="cols-3 style-none">
<li><code>+</code></li>
<li><code>-</code></li>
<li><code>*</code></li>
<li><code>/</code></li>
<li><code>%</code></li>
<li><code>**</code></li>
</ul>
<!--rehype:className=cols-3 style-none-->
<h4 id="比较运算符"><a aria-hidden="true" tabindex="-1" href="#比较运算符"><span class="icon icon-link"></span></a>比较运算符</h4>
<ul class="cols-3 style-none">
<li><code>==</code></li>
<li><code>!=</code></li>
<li><code>></code></li>
<li><code>&#x3C;</code></li>
<li><code>>=</code></li>
<li><code>&#x3C;=</code></li>
<li><code>&#x3C;=></code></li>
<li><code>===</code></li>
<li><code>eql?</code></li>
<li><code>equal?</code></li>
</ul>
<!--rehype:className=cols-3 style-none-->
<h4 id="运算符示例"><a aria-hidden="true" tabindex="-1" href="#运算符示例"><span class="icon icon-link"></span></a>运算符示例</h4>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 添加</span>
</span><span class="code-line"><span class="token number">1</span> + <span class="token number">1</span>   <span class="token comment">#=> 2</span>
</span><span class="code-line"><span class="token comment"># 减法</span>
</span><span class="code-line"><span class="token number">2</span> - <span class="token number">1</span>   <span class="token comment">#=> 1</span>
</span><span class="code-line"><span class="token comment"># 乘法</span>
</span><span class="code-line"><span class="token number">2</span> * <span class="token number">2</span>   <span class="token comment">#=> 4</span>
</span><span class="code-line"><span class="token comment"># 分配</span>
</span><span class="code-line"><span class="token number">10</span> / <span class="token number">5</span>  <span class="token comment">#=> 2</span>
</span><span class="code-line"><span class="token number">17</span> / <span class="token number">5</span>    <span class="token comment">#=> 3, not 3.4</span>
</span><span class="code-line"><span class="token number">17</span> / <span class="token number">5.0</span>  <span class="token comment">#=> 3.4</span>
</span><span class="code-line"><span class="token comment"># 指数</span>
</span><span class="code-line"><span class="token number">2</span> ** <span class="token number">2</span>  <span class="token comment">#=> 4</span>
</span><span class="code-line"><span class="token number">3</span> ** <span class="token number">4</span>  <span class="token comment">#=> 81</span>
</span><span class="code-line"><span class="token comment"># 模数(求除法的余数)</span>
</span><span class="code-line"><span class="token number">8</span> % <span class="token number">2</span>   <span class="token comment">#=> 0  (8 / 2 = 4; 没有剩余)</span>
</span><span class="code-line"><span class="token number">10</span> % <span class="token number">4</span>  <span class="token comment">#=> 2  (10 / 4 = 2 余数为 2)</span>
</span><span class="code-line">a <span class="token operator">=</span> <span class="token number">10</span>
</span><span class="code-line">b <span class="token operator">=</span> <span class="token number">20</span>
</span><span class="code-line">a <span class="token operator">==</span> b <span class="token comment">#=> false</span>
</span><span class="code-line">a <span class="token operator">!=</span> b <span class="token comment">#=> true</span>
</span><span class="code-line">a <span class="token operator">></span> b <span class="token comment">#=> false</span>
</span><span class="code-line">a <span class="token operator">&#x3C;</span> b <span class="token comment">#=> true</span>
</span><span class="code-line">a <span class="token operator">>=</span> b <span class="token comment">#=> false</span>
</span><span class="code-line">a <span class="token operator">&#x3C;=</span> b <span class="token comment">#=> true</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 比较运算符</span>
</span><span class="code-line">a <span class="token operator">&#x3C;=</span><span class="token operator">></span> b <span class="token comment">#=> -1</span>
</span><span class="code-line">c <span class="token operator">=</span> <span class="token number">20</span>
</span><span class="code-line">c <span class="token operator">&#x3C;=</span><span class="token operator">></span> b <span class="token comment">#=> 0</span>
</span><span class="code-line">c <span class="token operator">&#x3C;=</span><span class="token operator">></span> a  <span class="token comment">#=> 1</span>
</span><span class="code-line"><span class="token comment"># 用于测试 case 语句的 when 子句中的相等性</span>
</span><span class="code-line"><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">..</span>.10<span class="token punctuation">)</span> <span class="token operator">==</span><span class="token operator">=</span> <span class="token number">5</span> <span class="token comment">#=> true</span>
</span><span class="code-line"><span class="token comment"># 如果接收者和参数具有相同的类型和相等的值，则为真</span>
</span><span class="code-line"><span class="token number">1</span>.eql?<span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">)</span> <span class="token comment">#=> false</span>
</span><span class="code-line">c <span class="token operator">=</span> a + b  <span class="token comment">#=> 30</span>
</span><span class="code-line">c <span class="token operator">+=</span> a <span class="token comment">#=> 40</span>
</span><span class="code-line">c -<span class="token operator">=</span> a <span class="token comment">#=> 30</span>
</span><span class="code-line">c *<span class="token operator">=</span> a <span class="token comment">#=> 300</span>
</span><span class="code-line">c /<span class="token operator">=</span> a <span class="token comment">#=> 30</span>
</span><span class="code-line">c %<span class="token operator">=</span> a <span class="token comment">#=> 3</span>
</span><span class="code-line">c **<span class="token operator">=</span> a <span class="token comment">#=> 59049</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># Ruby 并行赋值</span>
</span><span class="code-line">a <span class="token operator">=</span> <span class="token number">10</span>
</span><span class="code-line">b <span class="token operator">=</span> <span class="token number">20</span>
</span><span class="code-line">c <span class="token operator">=</span> <span class="token number">30</span>
</span><span class="code-line">a, b, c <span class="token operator">=</span> <span class="token number">10</span>, <span class="token number">20</span>, <span class="token number">30</span>
</span><span class="code-line"><span class="token comment"># Ruby 位运算符</span>
</span><span class="code-line">a <span class="token operator">=</span> <span class="token number">60</span>
</span><span class="code-line">b <span class="token operator">=</span> <span class="token number">13</span>
</span><span class="code-line"><span class="token comment"># &#x26; 如果两个操作数中都存在，则二进制 AND 运算符将位复制到结果中。</span>
</span><span class="code-line">a <span class="token operator">&#x26;</span> b <span class="token comment">#=> 12</span>
</span><span class="code-line"><span class="token comment"># | 如果二进制或运算符存在于任一操作数中，则复制一个位。</span>
</span><span class="code-line">a <span class="token operator">|</span> b <span class="token comment">#=> 61</span>
</span><span class="code-line"><span class="token comment"># ^ 二元异或操作符如果在一个操作数中设置，则复制该位，但不能同时在两个操作数中设置。</span>
</span><span class="code-line">a ^ b <span class="token comment">#=> 49</span>
</span><span class="code-line"><span class="token comment"># ~ 二进制补码运算符是一元的，具有“翻转”位的效果。</span>
</span><span class="code-line">~a
</span><span class="code-line"><span class="token comment"># &#x3C;&#x3C; 二进制左移运算符。 左操作数的值被移动</span>
</span><span class="code-line"><span class="token comment"># 左操作数指定的位数。</span>
</span><span class="code-line">a <span class="token operator">&#x3C;&#x3C;</span> <span class="token number">2</span>
</span><span class="code-line"><span class="token comment"># >> 二进制右移运算符。 左操作数的值被移动</span>
</span><span class="code-line"><span class="token comment"># 右操作数指定的位数。</span>
</span><span class="code-line">a <span class="token operator">>></span> <span class="token number">2</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># Ruby 逻辑运算符</span>
</span><span class="code-line">a and b <span class="token comment">#=> true.</span>
</span><span class="code-line">a or b <span class="token comment">#=> true.</span>
</span><span class="code-line">a <span class="token operator">&#x26;&#x26;</span> b <span class="token comment">#=> true.</span>
</span><span class="code-line"><span class="token punctuation">(</span>a <span class="token operator">||</span> b<span class="token punctuation">)</span> <span class="token comment">#=> true.</span>
</span><span class="code-line"><span class="token operator">!</span><span class="token punctuation">(</span>a <span class="token operator">&#x26;&#x26;</span> b<span class="token punctuation">)</span> <span class="token comment">#=> false.</span>
</span><span class="code-line">not<span class="token punctuation">(</span>a <span class="token operator">&#x26;&#x26;</span> b<span class="token punctuation">)</span> <span class="token comment">#=> false.</span>
</span><span class="code-line"><span class="token comment"># Ruby 三元运算符</span>
</span><span class="code-line"><span class="token comment"># ? :</span>
</span><span class="code-line"><span class="token comment"># 如果条件为真？ 然后值 X ：否则值 Y</span>
</span><span class="code-line">a <span class="token operator">==</span> <span class="token number">10</span> ? puts <span class="token string">'Right'</span> <span class="token builtin class-name">:</span> puts <span class="token string">'Wrong'</span>
</span><span class="code-line"><span class="token comment"># Ruby 范围运算符</span>
</span><span class="code-line"><span class="token comment"># .. 创建从起点到终点的范围(含)</span>
</span><span class="code-line"><span class="token number">1</span><span class="token punctuation">..</span><span class="token number">10</span> <span class="token comment">#=> 创建从 1 到 10 的范围(包括 1 到 10)</span>
</span><span class="code-line"><span class="token comment"># ... 创建一个从起点到终点的范围，不包括在内</span>
</span><span class="code-line"><span class="token number">1</span><span class="token punctuation">..</span>.10 <span class="token comment">#=> 创建一个从 1 到 10 的独占范围</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="运算符优先级表"><a aria-hidden="true" tabindex="-1" href="#运算符优先级表"><span class="icon icon-link"></span></a>运算符优先级表</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<ul class="style-timeline">
<li><code>!</code>, <code>~</code>, <code>unary +</code></li>
<li><code>**</code></li>
<li><code>unary -</code></li>
<li><code>*</code>, <code>/</code>, <code>%</code></li>
<li><code>+</code>, <code>-</code></li>
<li><code>&#x3C;&#x3C;</code>, <code>>></code></li>
<li><code>&#x26;</code></li>
<li><code>^</code></li>
<li><code>></code>, <code>>=</code>, <code>&#x3C;</code>, <code>&#x3C;=</code></li>
<li><code>&#x3C;=></code>, <code>==</code>, <code>===</code>, <code>!=</code>, <code>=~</code>, <code>!~</code></li>
<li><code>&#x26;&#x26;</code></li>
<li><code>?</code>, <code>:</code></li>
<li><code>modifier-rescue</code></li>
<li><code>=</code>, <code>+=</code>, <code>-=</code>, <code>*=</code>, <code>/=</code>, <code>%=</code></li>
<li><code>defined</code></li>
<li><code>not</code></li>
<li><code>or</code>, <code>and</code></li>
<li><code>modifier-if</code>, <code>modifier-unless</code>, <code>modifier-while</code>, <code>modifier-until</code></li>
<li><code>{ }</code> 块</li>
<li><code>do ... end</code> 块</li>
</ul>
<!--rehype:className=style-timeline-->
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="变量和范围"><a aria-hidden="true" tabindex="-1" href="#变量和范围"><span class="icon icon-link"></span></a>变量和范围</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->









































<table><thead><tr><th align="left">名字</th><th>范围</th><th>示例</th><th>说明</th></tr></thead><tbody><tr><td align="left"><code>[a-z]</code> 或 <code>_</code></td><td>本地的</td><td><code>count = 10</code> 或 <code>_count = 10</code></td><td>必须初始化局部变量</td></tr><tr><td align="left"><code>@</code></td><td>实例变量</td><td><code>@id = []</code></td><td>实例变量在初始化之前具有“nil”值</td></tr><tr><td align="left"><code>@@</code></td><td>类变量</td><td><code>@@name = []</code></td><td>必须初始化类变量</td></tr><tr><td align="left"><code>$</code></td><td>全局变量</td><td><code>$version = "0.8.9"</code></td><td>全局变量在初始化之前具有“nil”值</td></tr><tr><td align="left"><code>[A-Z]</code></td><td>持续的</td><td><code>PI = 3.14</code></td><td>常量变量必须初始化，您可以更改常量，但您会收到警告</td></tr></tbody></table>
<p>有五种不同类型的变量。第一个字符确定范围</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="局部变量"><a aria-hidden="true" tabindex="-1" href="#局部变量"><span class="icon icon-link"></span></a>局部变量</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">current_weather <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"rainy"</span></span>
</span><span class="code-line">_weather <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"sunny"</span></span>
</span></code></pre>
<p>必须以下划线或小写字母开头</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="实例变量"><a aria-hidden="true" tabindex="-1" href="#实例变量"><span class="icon icon-link"></span></a>实例变量</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token comment"># 实例类变量</span>
</span><span class="code-line"><span class="token variable">@current_weather</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"rainy"</span></span>
</span><span class="code-line"><span class="token comment"># 全局变量</span>
</span><span class="code-line"><span class="token variable">$current_weather</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"rainy"</span></span>
</span><span class="code-line"><span class="token comment"># 常量变量</span>
</span><span class="code-line"><span class="token constant">WEATHER</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"rainy"</span></span><span class="token punctuation">.</span>freeze
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="伪变量"><a aria-hidden="true" tabindex="-1" href="#伪变量"><span class="icon icon-link"></span></a>伪变量</h3><div class="wrap-body">

































<table><thead><tr><th align="left">名字</th><th>说明</th></tr></thead><tbody><tr><td align="left"><code>self</code></td><td>当前方法的接收者对象</td></tr><tr><td align="left"><code>true</code></td><td><code>TrueClass</code> 的实例</td></tr><tr><td align="left"><code>false</code></td><td><code>FalseClass</code> 的实例</td></tr><tr><td align="left"><code>nil</code></td><td><code>NilClass</code> 的实例</td></tr><tr><td align="left"><code>__FILE__</code></td><td>当前源文件名</td></tr><tr><td align="left"><code>__LINE__</code></td><td>当前源文件的当前行号</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="选项变量"><a aria-hidden="true" tabindex="-1" href="#选项变量"><span class="icon icon-link"></span></a>选项变量</h3><div class="wrap-body">













































<table><thead><tr><th align="left">名字</th><th>说明</th></tr></thead><tbody><tr><td align="left"><code>$-0</code></td><td><code>$/</code> 的别名</td></tr><tr><td align="left"><code>$-a</code></td><td>如果设置了选项 <code>-a</code>，则为真。只读变量</td></tr><tr><td align="left"><code>$-d</code></td><td><code>$DEBUG</code> 的别名</td></tr><tr><td align="left"><code>$-F</code></td><td><code>$;</code> 的别名</td></tr><tr><td align="left"><code>$-i</code></td><td>在就地编辑模式下，此变量保存扩展，否则为零<br>可以指定启用（或禁用）就地编辑模式</td></tr><tr><td align="left"><code>$-I</code></td><td><code>$:</code> 的别名</td></tr><tr><td align="left"><code>$-l</code></td><td>如果选项 <code>-lis</code> 设置为真。只读变量</td></tr><tr><td align="left"><code>$-p</code></td><td>如果选项 <code>-pi</code> 设置为真。只读变量</td></tr><tr><td align="left"><code>$-v</code></td><td><code>$VERBOSE</code> 的别名</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="预定义变量"><a aria-hidden="true" tabindex="-1" href="#预定义变量"><span class="icon icon-link"></span></a>预定义变量</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-3-->

































































































































<table><thead><tr><th align="left">名字</th><th>说明</th></tr></thead><tbody><tr><td align="left"><code>$!</code></td><td>异常信息消息。raise 设置此变量</td></tr><tr><td align="left"><code>$@</code></td><td>最后一个异常的回溯，它是 String 的数组，指示调用方法的位置。格式中的元素如：“filename:line”或“filename:line:in `methodname'”(助记符：发生异常的地方)</td></tr><tr><td align="left"><code>$&#x26;</code></td><td>与此范围内最后一次成功的模式匹配匹配的字符串，如果最后一次模式匹配失败，则返回 nil。 (助记符：在某些编辑器中类似于 &#x26;)这个变量是只读的</td></tr><tr><td align="left"><code>$`</code></td><td>当前范围内最后一次成功的模式匹配所匹配的任何内容之前的字符串，如果最后一次模式匹配失败，则为 nil。 (助记符：` 通常在带引号的字符串之前)此变量是只读的</td></tr><tr><td align="left"><code>$'</code></td><td>当前范围内最后一次成功的模式匹配所匹配的字符串后面的字符串，如果最后一次模式匹配失败，则为 nil。 (助记符：' 通常跟在带引号的字符串之后)</td></tr><tr><td align="left"><code>$+</code></td><td>最后一个成功的搜索模式匹配的最后一个括号，如果最后一个模式匹配失败，则为 nil。如果您不知道一组替代模式中的哪一个匹配，这很有用。 (助记：积极向上)</td></tr><tr><td align="left"><code>$1</code>, <code>$2...</code></td><td>包含上一次成功匹配的模式中相应括号集中的子模式，不计算已经退出的嵌套块中匹配的模式，或者如果最后一次模式匹配失败，则为 nil。 (助记符：如 \digit)这些变量都是只读的</td></tr><tr><td align="left"><code>$~</code></td><td>当前范围内最后一个匹配的信息。设置此变量会影响匹配变量，如 <code>$&#x26;、$+、$1、$2..</code> 等。第 n 个子表达式可以通过 <code>$~[nth]</code> 检索。 (助记符：<code>~</code> 用于匹配)这个变量是局部作用域的</td></tr><tr><td align="left"><code>$=</code></td><td>不区分大小写的标志，默认为 nil。 (助记符：= 用于比较)</td></tr><tr><td align="left"><code>$/</code></td><td>输入记录分隔符，默认为换行符。像 awk 的 RS 变量一样工作。如果设置为 nil，则将立即读取整个文件。 (助记符：/ 用于在引用诗歌时划定行界)</td></tr><tr><td align="left"><code>$\</code></td><td>print 和 IO#write 的输出记录分隔符。默认值为无。 (助记符：它就像 /，但它是你从 Ruby 中“返回”的东西)</td></tr><tr><td align="left"><code>$,</code></td><td>打印的输出字段分隔符。此外，它是 Array#join 的默认分隔符。 (助记符：当您的打印语句中有 , 时打印的内容)</td></tr><tr><td align="left"><code>$;</code></td><td>String#split 的默认分隔符。</td></tr><tr><td align="left"><code>$.</code></td><td>读取的最后一个文件的当前输入行号。</td></tr><tr><td align="left"><code>$&#x3C;</code></td><td>由命令行参数或标准输入给出的文件的虚拟连接文件(如果没有提供参数文件)。 <code>$&#x3C;.file</code> 返回当前文件名。 (助记符：<code>$&#x3C;</code> 是一个 shell 输入源)</td></tr><tr><td align="left"><code>$></code></td><td><code>print</code> 的默认输出，<code>printf</code>。 <code>$stdout</code> 默认情况下。 (助记符：<code>$></code> 用于 shell 输出)</td></tr><tr><td align="left"><code>$_</code></td><td>通过gets或readline输入String的最后一行。如果gets/readline 遇到EOF，它被设置为nil。这个变量是局部作用域的。 (助记符：部分与 Perl 相同)</td></tr><tr><td align="left"><code>$0</code></td><td>包含包含正在执行的 Ruby 脚本的文件的名称。在某些操作系统上，分配给 $0 会修改 ps(1) 程序看到的参数区域。作为一种指示当前程序状态的方式，这比隐藏您正在运行的程序更有用。 (助记符：与 sh 和 ksh 相同)</td></tr><tr><td align="left"><code>$*</code></td><td>为脚本提供的命令行参数。 Ruby 解释器的选项已被删除。 (助记符：与 sh 和 ksh 相同)</td></tr><tr><td align="left"><code>$$</code></td><td>运行此脚本的 Ruby 的进程号。(助记符：与贝壳相同)</td></tr><tr><td align="left"><code>$?</code></td><td>最后执行的子进程的状态。</td></tr><tr><td align="left"><code>$:</code></td><td>该数组包含通过 load 或 require 查找 Ruby 脚本和二进制模块的位置列表。 它最初由任何 <code>-I</code> 命令行开关的参数组成，然后是默认的 Ruby 库，probabl <code>"/usr/local/lib/ruby"</code>，然后是 <code>"."</code>，表示当前目录 . (助记符：冒号是 PATH 环境变量的分隔符)</td></tr><tr><td align="left"><code>$"</code></td><td>该数组包含由 require 加载的模块名称。 用于防止 require 两次加载模块。助记符：防止文件被双引号(加载)</td></tr><tr><td align="left"><code>$DEBUG</code></td><td><code>-d</code> 开关的状态。</td></tr><tr><td align="left"><code>$FILENAME</code></td><td>与<code>$&#x3C;.filename</code> 相同</td></tr><tr><td align="left"><code>$LOAD_PATH</code></td><td><code>$:</code> 的别名</td></tr><tr><td align="left"><code>$stdin</code></td><td>当前的标准输入</td></tr><tr><td align="left"><code>$stdout</code></td><td>当前的标准输出</td></tr><tr><td align="left"><code>$stderr</code></td><td>当前标准错误输出</td></tr><tr><td align="left"><code>$VERBOSE</code></td><td>详细标志，由 <code>-v</code> 开关设置到 Ruby 解释器</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="预定义的全局常量"><a aria-hidden="true" tabindex="-1" href="#预定义的全局常量"><span class="icon icon-link"></span></a>预定义的全局常量</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->





























































<table><thead><tr><th align="left">名字</th><th>说明</th></tr></thead><tbody><tr><td align="left"><code>TRUE</code></td><td>典型的真值。在 Ruby 中，所有非 <code>false</code> 值（除了 <code>nil</code> 和 <code>false</code> 之外的所有值）都是 <code>true</code></td></tr><tr><td align="left"><code>FALSE</code></td><td>虚假本身</td></tr><tr><td align="left"><code>NIL</code></td><td>零本身</td></tr><tr><td align="left"><code>STDIN</code></td><td>标准输入。<code>$stdin</code> 默认值</td></tr><tr><td align="left"><code>STDOUT</code></td><td>标准输出。<code>$stdout</code> 默认值</td></tr><tr><td align="left"><code>STDERR</code></td><td>标准错误输出。<code>$stderr</code> 默认值</td></tr><tr><td align="left"><code>ENV</code></td><td>类哈希对象包含当前环境变量。 在 ENV 中设置值会更改子进程的环境</td></tr><tr><td align="left"><code>ARGF</code></td><td><code>$&#x3C;</code> 的别名</td></tr><tr><td align="left"><code>ARGV</code></td><td><code>$*</code> 的别名</td></tr><tr><td align="left"><code>DATA</code></td><td>脚本的文件对象，就在 <strong>END</strong> 之后。 除非未从文件中读取脚本，否则未定义</td></tr><tr><td align="left"><code>VERSION</code></td><td>Ruby 版本字符串</td></tr><tr><td align="left"><code>RUBY_RELEASE_DATE</code></td><td>发布日期字符串</td></tr><tr><td align="left"><code>RUBY_PLATFORM</code></td><td>平台标识符</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="检查变量的范围"><a aria-hidden="true" tabindex="-1" href="#检查变量的范围"><span class="icon icon-link"></span></a>检查变量的范围</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">defined</span><span class="token operator">?</span> count
</span><span class="code-line"><span class="token string-literal"><span class="token string">"local-variable"</span></span>
</span><span class="code-line"><span class="token keyword">defined</span><span class="token operator">?</span> <span class="token variable">@id</span>
</span><span class="code-line"><span class="token string-literal"><span class="token string">"instance-variable"</span></span>
</span><span class="code-line"><span class="token keyword">defined</span><span class="token operator">?</span> <span class="token variable">@@name</span>
</span><span class="code-line"><span class="token string-literal"><span class="token string">"class variable"</span></span>
</span><span class="code-line"><span class="token keyword">defined</span><span class="token operator">?</span> <span class="token variable">$version</span>
</span><span class="code-line"><span class="token string-literal"><span class="token string">"global-variable"</span></span>
</span><span class="code-line"><span class="token keyword">defined</span><span class="token operator">?</span> <span class="token constant">PI</span>
</span><span class="code-line"><span class="token string-literal"><span class="token string">"constant"</span></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2 row-span-3"><div class="wrap-header h3wrap"><h3 id="数据类型"><a aria-hidden="true" tabindex="-1" href="#数据类型"><span class="icon icon-link"></span></a>数据类型</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-3-->

































































<table class="show-header"><thead><tr><th align="left">类型</th><th>示例</th><th>Class</th><th>文档</th></tr></thead><tbody><tr><td align="left"><code>Integer</code></td><td>a = 17</td><td>a.class > Integer <br>a.class.superclass > Numeric</td><td><a href="https://ruby-doc.org/core-3.1.2/Integer.html">#</a></td></tr><tr><td align="left"><code>Float</code></td><td>a = 87.23</td><td>a.class > Float <br>a.class.superclass > Numeric</td><td><a href="https://ruby-doc.org/core-3.1.2/Float.html">#</a></td></tr><tr><td align="left"><code>String</code></td><td>a = "Hello universe"</td><td>a.class > String</td><td><a href="https://ruby-doc.org/core-3.1.2/String.html">#</a></td></tr><tr><td align="left"><code>Array</code></td><td>a = [12, 34]</td><td>a.class > Array</td><td><a href="https://ruby-doc.org/core-3.1.2/Array.html">#</a></td></tr><tr><td align="left"><code>Hash</code></td><td>a = {type: "tea", count: 10}</td><td>a.class > Hash</td><td><a href="https://ruby-doc.org/core-3.1.2/Hash.html">#</a></td></tr><tr><td align="left"><code>Boolean</code></td><td>a = false<br>a = true</td><td>a.class > FalseClass <br>a.class > TrueClass</td><td><a href="https://ruby-doc.org/core-3.1.2/TrueClass.html">TrueClass</a> <a href="https://ruby-doc.org/core-3.1.2/FalseClass.html">FalseClass</a></td></tr><tr><td align="left"><code>Symbol</code></td><td>a = :status</td><td>a.class > Symbol</td><td><a href="https://ruby-doc.org/core-3.1.2/Symbol.html">#</a></td></tr><tr><td align="left"><code>Range</code></td><td>a = 1..3</td><td>a.class > Range</td><td><a href="https://ruby-doc.org/core-3.1.2/Range.html">#</a></td></tr><tr><td align="left"><code>Nil</code></td><td>a = nil</td><td>a.class > NilClass</td><td><a href="https://ruby-doc.org/core-3.1.2/NilClass.html">#</a></td></tr></tbody></table>
<!--rehype:className=show-header-->
<p><a href="https://www.digitalocean.com/community/tutorials/understanding-data-types-in-ruby">进一步阅读</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="检查数据类型"><a aria-hidden="true" tabindex="-1" href="#检查数据类型"><span class="icon icon-link"></span></a>检查数据类型</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token comment"># 两者都是同义词</span>
</span><span class="code-line">a <span class="token operator">=</span> <span class="token number">37</span>
</span><span class="code-line">a<span class="token punctuation">.</span>kind_of<span class="token operator">?</span> <span class="token builtin">Integer</span>
</span><span class="code-line"><span class="token comment"># true</span>
</span><span class="code-line">a<span class="token punctuation">.</span>is_a<span class="token operator">?</span> <span class="token builtin">Integer</span>
</span><span class="code-line"><span class="token comment"># true</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="symbol"><a aria-hidden="true" tabindex="-1" href="#symbol"><span class="icon icon-link"></span></a>Symbol</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">week_days <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token symbol">sunday</span><span class="token operator">:</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token symbol">monday</span><span class="token operator">:</span> <span class="token number">222</span><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="整数有用的方法"><a aria-hidden="true" tabindex="-1" href="#整数有用的方法"><span class="icon icon-link"></span></a>整数有用的方法</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token number">2.</span>even<span class="token operator">?</span>
</span><span class="code-line"><span class="token comment"># true</span>
</span><span class="code-line"><span class="token number">3.</span>even<span class="token operator">?</span>
</span><span class="code-line"><span class="token comment"># false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="范围"><a aria-hidden="true" tabindex="-1" href="#范围"><span class="icon icon-link"></span></a>范围</h3><div class="wrap-body">
<p><code>..</code> 用于创建包含范围</p>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">range <span class="token operator">=</span> <span class="token number">1.</span><span class="token number">.10</span>
</span><span class="code-line">range<span class="token punctuation">.</span>to_a
</span><span class="code-line"><span class="token comment"># 输出 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span>
</span></code></pre>
<p><code>...</code> 用于创建专属范围</p>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">range <span class="token operator">=</span> <span class="token number">1.</span><span class="token punctuation">.</span><span class="token number">.10</span>
</span><span class="code-line">range<span class="token punctuation">.</span>to_a
</span><span class="code-line"><span class="token comment"># 输出 => [1, 2, 3, 4, 5, 6, 7, 8, 9]</span>
</span></code></pre>
<p>一些有用的方法</p>





























<table><thead><tr><th align="left">Method name</th><th>Output</th></tr></thead><tbody><tr><td align="left"><code>cover?</code></td><td><code>(1..5).cover?(5)</code> => <code>true</code></td></tr><tr><td align="left"><code>end</code></td><td><code>('a'..'z').end</code> => <code>"z"</code></td></tr><tr><td align="left"><code>first</code></td><td><code>(1..5).first</code> => <code>1</code></td></tr><tr><td align="left"><code>first(3)</code></td><td><code>('A'..'Z').first(2)</code> => <code>["A", "B"]</code></td></tr><tr><td align="left"><code>eql?</code></td><td><code>((0..2).eql?(0..5)</code> => <code>false</code></td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="在-range-中使用-step"><a aria-hidden="true" tabindex="-1" href="#在-range-中使用-step"><span class="icon icon-link"></span></a>在 Range 中使用 step</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token punctuation">(</span><span class="token number">1.</span><span class="token number">.20</span><span class="token punctuation">)</span><span class="token punctuation">.</span>step<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">|</span>number<span class="token operator">|</span> puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">number</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 1</span>
</span><span class="code-line"><span class="token comment"># 3</span>
</span><span class="code-line"><span class="token comment"># 5</span>
</span><span class="code-line"><span class="token comment"># 7</span>
</span><span class="code-line"><span class="token comment"># 9</span>
</span><span class="code-line"><span class="token comment"># 11</span>
</span><span class="code-line"><span class="token comment"># 13</span>
</span><span class="code-line"><span class="token comment"># 15</span>
</span><span class="code-line"><span class="token comment"># 17</span>
</span><span class="code-line"><span class="token comment"># 19</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="条件结构"><a aria-hidden="true" tabindex="-1" href="#条件结构"><span class="icon icon-link"></span></a>条件结构</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="if-修饰符"><a aria-hidden="true" tabindex="-1" href="#if-修饰符"><span class="icon icon-link"></span></a>if 修饰符</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">num <span class="token operator">=</span> <span class="token number">2</span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">'two'</span></span> <span class="token keyword">if</span> num <span class="token operator">==</span> <span class="token number">2</span> 
</span></code></pre>
<p>如果条件为真，则执行代码</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="if-elsif-else-语句"><a aria-hidden="true" tabindex="-1" href="#if-elsif-else-语句"><span class="icon icon-link"></span></a>if elsif else 语句</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">temp <span class="token operator">=</span> <span class="token number">19</span>
</span><span class="code-line"><span class="token keyword">if</span> temp <span class="token operator">>=</span> <span class="token number">25</span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"hot"</span></span>
</span><span class="code-line"><span class="token keyword">elsif</span> temp <span class="token operator">&#x3C;</span> <span class="token number">25</span> <span class="token operator">&#x26;&#x26;</span> temp <span class="token operator">>=</span> <span class="token number">18</span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"normal"</span></span>
</span><span class="code-line"><span class="token keyword">else</span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"cold"</span></span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line"><span class="token comment"># 输出 => normal</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="除非语句"><a aria-hidden="true" tabindex="-1" href="#除非语句"><span class="icon icon-link"></span></a>除非语句</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token comment"># 除非与 if 相反，当语句为假时进行评估</span>
</span><span class="code-line">name <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"rob"</span></span>
</span><span class="code-line"><span class="token comment"># if name != "bob"</span>
</span><span class="code-line"><span class="token keyword">unless</span> name <span class="token operator">==</span> <span class="token string-literal"><span class="token string">"bob"</span></span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"hello stranger"</span></span>
</span><span class="code-line"><span class="token keyword">else</span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"hello bob"</span></span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line"><span class="token comment"># 输出 => hello stranger</span>
</span><span class="code-line">num <span class="token operator">=</span> <span class="token number">6</span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">'not two'</span></span> <span class="token keyword">unless</span> num <span class="token operator">==</span> <span class="token number">2</span>
</span><span class="code-line"><span class="token comment"># 输出 => not two</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="case-陈述"><a aria-hidden="true" tabindex="-1" href="#case-陈述"><span class="icon icon-link"></span></a>case 陈述</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token comment"># case 返回最后执行的表达式的值</span>
</span><span class="code-line"><span class="token keyword">case</span> input
</span><span class="code-line"><span class="token comment"># 检查一个整数，19</span>
</span><span class="code-line"><span class="token keyword">when</span> <span class="token number">19</span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"It's 19"</span></span>
</span><span class="code-line">  <span class="token comment"># 检查浮点数，33.3</span>
</span><span class="code-line"><span class="token keyword">when</span> <span class="token number">33.3</span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"It's 33.3"</span></span>
</span><span class="code-line">  <span class="token comment"># 检查一个确切的字符串，“Zaman”</span>
</span><span class="code-line"><span class="token keyword">when</span> <span class="token string-literal"><span class="token string">"Zaman"</span></span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"Hi Zaman"</span></span>
</span><span class="code-line"><span class="token keyword">when</span> <span class="token number">10</span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"It's 10"</span></span>
</span><span class="code-line">  <span class="token comment"># 检查范围</span>
</span><span class="code-line"><span class="token keyword">when</span> <span class="token number">7.</span><span class="token number">.11</span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"It's between 7 and 11"</span></span>
</span><span class="code-line">  <span class="token comment"># 检查多个值，“咖啡”</span>
</span><span class="code-line"><span class="token keyword">when</span> <span class="token string-literal"><span class="token string">"tea"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"coffee"</span></span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"Happy days"</span></span>
</span><span class="code-line">  <span class="token comment"># 检查正则表达式“aA6”</span>
</span><span class="code-line"><span class="token keyword">when</span> <span class="token regex-literal"><span class="token regex">/^a[A-Z]+[0-6]+$/</span></span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"It's a valid match"</span></span>
</span><span class="code-line">  <span class="token comment"># 通过与 String 类“任何字符串”</span>
</span><span class="code-line">  <span class="token comment"># 进行比较来检查任何字符串</span>
</span><span class="code-line"><span class="token keyword">when</span> <span class="token builtin">String</span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"It's a String"</span></span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="case-简短的语法"><a aria-hidden="true" tabindex="-1" href="#case-简短的语法"><span class="icon icon-link"></span></a>case 简短的语法</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">case</span> input
</span><span class="code-line">  <span class="token keyword">when</span> <span class="token number">19</span> <span class="token keyword">then</span> puts <span class="token string-literal"><span class="token string">"It's 19"</span></span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="case-可选的失败"><a aria-hidden="true" tabindex="-1" href="#case-可选的失败"><span class="icon icon-link"></span></a>case 可选的失败</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">case</span> input
</span><span class="code-line">  <span class="token keyword">when</span> <span class="token number">19</span> <span class="token keyword">then</span> puts <span class="token string-literal"><span class="token string">"It's 19"</span></span>
</span><span class="code-line"><span class="token keyword">else</span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"It's not 19"</span></span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="case-获取返回值"><a aria-hidden="true" tabindex="-1" href="#case-获取返回值"><span class="icon icon-link"></span></a>case 获取返回值</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">marks <span class="token operator">=</span> <span class="token number">86</span>
</span><span class="code-line">result <span class="token operator">=</span> <span class="token keyword">case</span> marks
</span><span class="code-line">        <span class="token keyword">when</span> <span class="token number">0.</span><span class="token number">.49</span> <span class="token keyword">then</span> <span class="token string-literal"><span class="token string">"Fail"</span></span>
</span><span class="code-line">        <span class="token keyword">when</span> <span class="token number">50.</span><span class="token number">.64</span> <span class="token keyword">then</span> <span class="token string-literal"><span class="token string">"Pass"</span></span>
</span><span class="code-line">        <span class="token keyword">when</span> <span class="token number">65.</span><span class="token number">.74</span> <span class="token keyword">then</span> <span class="token string-literal"><span class="token string">"Credit"</span></span>
</span><span class="code-line">        <span class="token keyword">when</span> <span class="token number">75.</span><span class="token number">.84</span> <span class="token keyword">then</span> <span class="token string-literal"><span class="token string">"Distinction"</span></span>
</span><span class="code-line">        <span class="token keyword">when</span> <span class="token number">85.</span><span class="token number">.100</span> <span class="token keyword">then</span> <span class="token string-literal"><span class="token string">"High Distinction"</span></span>
</span><span class="code-line">        <span class="token keyword">else</span> <span class="token string-literal"><span class="token string">"Invalid marks"</span></span>
</span><span class="code-line">        <span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">puts result
</span><span class="code-line"><span class="token comment"># High Distinction</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="字符串"><a aria-hidden="true" tabindex="-1" href="#字符串"><span class="icon icon-link"></span></a>字符串</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="字符串插值"><a aria-hidden="true" tabindex="-1" href="#字符串插值"><span class="icon icon-link"></span></a>字符串插值</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">name <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"World"</span></span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"Hello </span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">name</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"The total is </span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content"><span class="token number">1</span><span class="token operator">+</span><span class="token number">1</span></span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line"><span class="token comment"># "the total is 2"</span>
</span></code></pre>
<p>字符串插值允许您将字符串组合在一起</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="提取子字符串"><a aria-hidden="true" tabindex="-1" href="#提取子字符串"><span class="icon icon-link"></span></a>提取子字符串</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">string <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"abc123"</span></span>
</span><span class="code-line">string<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># "abc"</span>
</span><span class="code-line">string<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># "123"</span>
</span><span class="code-line">string<span class="token punctuation">[</span><span class="token number">0.</span><span class="token punctuation">.</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># "abc12"</span>
</span><span class="code-line"><span class="token comment">#remove or replace the substring</span>
</span><span class="code-line">string<span class="token punctuation">[</span><span class="token number">0.</span><span class="token number">.2</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">""</span></span>
</span><span class="code-line">puts string
</span><span class="code-line"><span class="token comment"># "123"</span>
</span></code></pre>
<p>子字符串是字符串的一小部分，如果你只想要那个特定的部分，它会很有用，比如开头、中间或结尾</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="将字符串转换为小写或大写"><a aria-hidden="true" tabindex="-1" href="#将字符串转换为小写或大写"><span class="icon icon-link"></span></a>将字符串转换为小写或大写</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token string-literal"><span class="token string">"HELLO World"</span></span><span class="token punctuation">.</span>downcase  <span class="token comment"># "hello world"</span>
</span><span class="code-line"><span class="token string-literal"><span class="token string">"hello worlD"</span></span><span class="token punctuation">.</span>upcase    <span class="token comment"># "HELLO WORLD"</span>
</span><span class="code-line"><span class="token string-literal"><span class="token string">"hEllo wOrlD"</span></span><span class="token punctuation">.</span>capitalize <span class="token comment"># "Hello world"</span>
</span><span class="code-line"><span class="token string-literal"><span class="token string">"hEllo WOrlD"</span></span><span class="token punctuation">.</span>swapcase  <span class="token comment"># "HeLLO woRLd"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="有用的方法"><a aria-hidden="true" tabindex="-1" href="#有用的方法"><span class="icon icon-link"></span></a>有用的方法</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-3-->






















































































































































<table><thead><tr><th align="left">函数名称</th><th>Output</th><th>Note</th></tr></thead><tbody><tr><td align="left">length or size</td><td><code>"HELLO World".length</code> => <code>11</code> <br> <code>"HELLO World".size</code> => <code>11</code></td><td>返回字符串的长度</td></tr><tr><td align="left">reverse</td><td><code>"hello worlD".reverse</code> => <code>"Dlrow olleh"</code></td><td>返回反转的字符串</td></tr><tr><td align="left">include? other_str</td><td><code>"hEllo wOrlD".include? "w"</code> => <code>true</code></td><td>如果字符串或字符存在则返回 true，否则返回 false</td></tr><tr><td align="left">gsub(pattern, replacement)</td><td><code>"hEllo wOrlD".gsub(" ", "_")</code> => <code>"hEllo_wOrlD"</code></td><td>gsub 或全局替换用提供的字符串替换一个或多个字符串</td></tr><tr><td align="left">gsub(pattern, hash)</td><td><code>"organization".gsub("z", 'z' => 's')</code> => <code>"organisation"</code></td><td>gsub 或全局替换用提供的哈希替换一个或多个字符串</td></tr><tr><td align="left">gsub(pattern) { |match| block}</td><td><code>"Price of the phone is 1000 AUD".gsub(/\d+/) { |s| '$'+s }</code> <br> <code>"Price of the phone is $1000 AUD"</code></td><td>gsub 或全局替换用提供的块替换一个或多个字符串</td></tr><tr><td align="left">strip</td><td><code>" hEllo WOrlD ".strip</code> <br> <code>"hEllo WOrlD"</code></td><td>它将删除（修剪）以下任何前导和尾随字符：null（“\x00”）、水平制表符（“\t”）、换行符（\n）、垂直制表符（“\v”）、换页符(f)、回车(\r)、空格(" ")</td></tr><tr><td align="left">prepend</td><td><code>a = "world" &#x3C;br> a.prepend("hello ")</code> <br> <code>"hello world"</code></td><td>在另一个字符串之前添加字符串</td></tr><tr><td align="left">insert</td><td><code>a = "hello" &#x3C;br> a.insert(a.length, " world")</code> <br> <code>"hello world"</code></td><td>在特定位置插入字符串</td></tr><tr><td align="left">start_with?</td><td><code>string = "ruby programming"</code> <br> <code>string.start_with? "ruby"</code> <br> <code>true</code></td><td>检查字符串是否以特定前缀开头</td></tr><tr><td align="left">end_with?</td><td><code>string = "ruby programming"</code> <br> <code>string.end_with? "ruby"</code> <br> <code>false</code></td><td>检查字符串是否以特定前缀结尾</td></tr><tr><td align="left">delete_suffix</td><td><code>string = "sausage is expensive"</code> <br> <code>string.delete_suffix(" is expensive")</code> <br> <code>"sausage"</code></td><td>从字符串中删除后缀</td></tr><tr><td align="left">delete_prefix</td><td><code>string = "sausage is expensive"</code> <br> <code>string.delete_prefix("sausage")</code> <br> <code>" is expensive"</code></td><td>从字符串中删除前缀</td></tr><tr><td align="left">split</td><td><code>string = "a b c d" &#x3C;br> string.split</code> <br> <code>["a", "b", "c", "d"]</code></td><td>将字符串转换为字符数组</td></tr><tr><td align="left">join</td><td><code>arr = ['a', 'b', 'c'] &#x3C;br> arr.join</code>  => <code>"abc"</code></td><td>将数组转换为字符串</td></tr><tr><td align="left">to_i</td><td><code>a = "49" &#x3C;br> a.to_i</code> => <code>49</code></td><td>将字符串转换为整数</td></tr><tr><td align="left">chop</td><td><code>"abcd?".chop("?")</code> => <code>"abcd"</code></td><td>从字符串中删除最后一个字符</td></tr><tr><td align="left">count</td><td><code>str = "aaab" &#x3C;br> str.count("a")</code> <br> <code>3</code></td><td>计算字符串中的字符</td></tr><tr><td align="left">to_f</td><td><code>a = "49"</code> <br> <code>a.to_f</code> <br> <code>49.0</code></td><td>将字符串转换为浮点数</td></tr><tr><td align="left">to_sym</td><td><code>a = "key"</code> <br> <code>a.to_sym</code> <br> <code>:key</code></td><td>将字符串转换为符号</td></tr><tr><td align="left">match</td><td><code>"abcd?".match(/ab/)</code> => <code>#&#x3C;MatchData "ab"></code></td><td>将模式转换为正则表达式并在字符串上调用其匹配方法</td></tr><tr><td align="left">empty?</td><td><code>"hello".empty?</code> => <code>false</code></td><td>如果字符串的长度为零，则返回 true</td></tr><tr><td align="left">squeeze</td><td><code>"Booook".squeeze</code> => <code>"Bok"</code></td><td>返回字符串的副本，其中相同字符的运行被单个字符替换</td></tr><tr><td align="left">*</td><td><code>puts "Ruby " * 4</code> => <code>Ruby Ruby Ruby Ruby</code></td><td>返回多个 self 副本的串联</td></tr><tr><td align="left">+</td><td><code>"sammy " + "shark"</code> => <code>"sammyshark"</code></td><td>返回 self 和给定的其他字符串的连接</td></tr><tr><td align="left">eql?</td><td><code>s = 'foo'</code> => <code>true</code> <br> <code>s.eql?('foo')</code> => <code>true</code></td><td>如果对象具有相同的长度和内容，则返回 true；作为自己；否则为假</td></tr><tr><td align="left">+</td><td><code>"sammy " + "shark"</code> => <code>"sammyshark"</code></td><td>返回 self 和给定的其他字符串的连接</td></tr><tr><td align="left">+</td><td><code>"sammy " + "shark"</code> => <code>"sammyshark"</code></td><td>返回 self 和给定的其他字符串的连接</td></tr></tbody></table>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="方法"><a aria-hidden="true" tabindex="-1" href="#方法"><span class="icon icon-link"></span></a>方法</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="声明一个方法"><a aria-hidden="true" tabindex="-1" href="#声明一个方法"><span class="icon icon-link"></span></a>声明一个方法</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">method_name</span></span><span class="token punctuation">(</span>parameter1<span class="token punctuation">,</span> parameter2<span class="token punctuation">)</span>
</span><span class="code-line">    puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">parameter1</span><span class="token delimiter punctuation">}</span></span><span class="token string"> </span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">parameter2</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line">    parameter1 <span class="token operator">+</span> parameter2
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
<hr>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">res <span class="token operator">=</span> method_name<span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># 输出 => 30</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">method_name</span></span><span class="token punctuation">(</span>parameter1<span class="token punctuation">,</span> parameter2<span class="token punctuation">)</span>
</span><span class="code-line">    puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">parameter1</span><span class="token delimiter punctuation">}</span></span><span class="token string"> </span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">parameter2</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line">    <span class="token keyword">return</span> parameter1 <span class="token operator">+</span> parameter2
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line"><span class="token comment"># 输出 => 30</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="调用方法"><a aria-hidden="true" tabindex="-1" href="#调用方法"><span class="icon icon-link"></span></a>调用方法</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">res <span class="token operator">=</span> method_name<span class="token punctuation">(</span>parameter1<span class="token punctuation">,</span> parameter2<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># 可以调用不带括号的方法</span>
</span><span class="code-line">res <span class="token operator">=</span> method_name parameter1<span class="token punctuation">,</span> parameter2
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-4"><div class="wrap-header h3wrap"><h3 id="类方法"><a aria-hidden="true" tabindex="-1" href="#类方法"><span class="icon icon-link"></span></a>类方法</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-4-->
<p>类方法是类级别的方法。 有多种定义类方法的方法</p>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Mobile</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token method-definition"><span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">ring</span></span>
</span><span class="code-line">        <span class="token string-literal"><span class="token string">"ring ring ring..."</span></span>
</span><span class="code-line">    <span class="token keyword">end</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">Mobile<span class="token punctuation">.</span>ring
</span></code></pre>
<hr>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Mobile</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token method-definition"><span class="token class-name">Mobile</span><span class="token punctuation">.</span><span class="token function">ring</span></span>
</span><span class="code-line">        <span class="token string-literal"><span class="token string">"ring ring ring..."</span></span>
</span><span class="code-line">    <span class="token keyword">end</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">Mobile<span class="token punctuation">.</span>ring
</span></code></pre>
<hr>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Mobile</span>
</span><span class="code-line">    <span class="token keyword">class</span> <span class="token operator">&#x3C;&#x3C;</span> <span class="token keyword">self</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">ring</span></span>
</span><span class="code-line">        <span class="token string-literal"><span class="token string">"ring ring ring..."</span></span>
</span><span class="code-line">       <span class="token keyword">end</span>
</span><span class="code-line">    <span class="token keyword">end</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">Mobile<span class="token punctuation">.</span>ring
</span></code></pre>
<p>类方法是类对象的实例方法。 当创建一个新类时，“Class”类型的对象被初始化并分配给一个全局常量（在本例中为 Mobile）</p>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">Mobile <span class="token operator">=</span> <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token keyword">new</span> <span class="token keyword">do</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token method-definition"><span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">ring</span></span>
</span><span class="code-line">        <span class="token string-literal"><span class="token string">"ring ring ring..."</span></span>
</span><span class="code-line">    <span class="token keyword">end</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">Mobile<span class="token punctuation">.</span>ring
</span></code></pre>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">Mobile <span class="token operator">=</span> <span class="token class-name">Class</span><span class="token punctuation">.</span><span class="token keyword">new</span>
</span><span class="code-line"><span class="token keyword">class</span> <span class="token operator">&#x3C;&#x3C;</span> Mobile
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">ring</span></span>
</span><span class="code-line">        <span class="token string-literal"><span class="token string">"ring ring ring..."</span></span>
</span><span class="code-line">    <span class="token keyword">end</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">Mobile<span class="token punctuation">.</span>ring
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用另一个参数作为默认值"><a aria-hidden="true" tabindex="-1" href="#使用另一个参数作为默认值"><span class="icon icon-link"></span></a>使用另一个参数作为默认值</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">method_name</span></span><span class="token punctuation">(</span>num1<span class="token punctuation">,</span> num2 <span class="token operator">=</span> num1<span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">return</span> num1 <span class="token operator">+</span> num2
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">res <span class="token operator">=</span> method_name<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># 输出 => 20</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="为方法参数定义默认值"><a aria-hidden="true" tabindex="-1" href="#为方法参数定义默认值"><span class="icon icon-link"></span></a>为方法参数定义默认值</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">method_name</span></span><span class="token punctuation">(</span>parameter1<span class="token punctuation">,</span> parameter2<span class="token punctuation">,</span> type <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"ADD"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">    puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">parameter1</span><span class="token delimiter punctuation">}</span></span><span class="token string"> </span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">parameter2</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line">    <span class="token keyword">return</span> parameter1 <span class="token operator">+</span> parameter2 <span class="token keyword">if</span> type <span class="token operator">==</span> <span class="token string-literal"><span class="token string">"ADD"</span></span>
</span><span class="code-line">    <span class="token keyword">return</span> parameter1 <span class="token operator">-</span> parameter2 <span class="token keyword">if</span> type <span class="token operator">==</span> <span class="token string-literal"><span class="token string">"SUB"</span></span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">res <span class="token operator">=</span> method_name<span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># 输出 => 30</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="将可变长度参数传递给方法参数"><a aria-hidden="true" tabindex="-1" href="#将可变长度参数传递给方法参数"><span class="icon icon-link"></span></a>将可变长度参数传递给方法参数</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">method_name</span></span><span class="token punctuation">(</span>type<span class="token punctuation">,</span> <span class="token operator">*</span>values<span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">return</span> values<span class="token punctuation">.</span>reduce<span class="token punctuation">(</span><span class="token operator">:</span><span class="token operator">+</span><span class="token punctuation">)</span> <span class="token keyword">if</span> type <span class="token operator">==</span> <span class="token string-literal"><span class="token string">"ADD"</span></span>
</span><span class="code-line">    <span class="token keyword">return</span> values<span class="token punctuation">.</span>reduce<span class="token punctuation">(</span><span class="token operator">:</span><span class="token operator">-</span><span class="token punctuation">)</span> <span class="token keyword">if</span> type <span class="token operator">==</span> <span class="token string-literal"><span class="token string">"SUB"</span></span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>
</span><span class="code-line">res <span class="token operator">=</span> method_name<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"ADD"</span></span><span class="token punctuation">,</span> <span class="token operator">*</span>numbers<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># 输出 => 15</span>
</span><span class="code-line">res <span class="token operator">=</span> method_name<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"SUB"</span></span><span class="token punctuation">,</span> <span class="token operator">*</span>numbers<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># 输出 => -11</span>
</span><span class="code-line"><span class="token comment"># 或者您可以提供这样的值</span>
</span><span class="code-line">res <span class="token operator">=</span> method_name<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"ADD"</span></span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># 输出 => 15</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="修改对象"><a aria-hidden="true" tabindex="-1" href="#修改对象"><span class="icon icon-link"></span></a>修改对象</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Drama"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Mystery"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Crime"</span></span><span class="token punctuation">,</span>
</span><span class="code-line"><span class="token string-literal"><span class="token string">"Sci-fi"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Disaster"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Thriller"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">a<span class="token punctuation">.</span>sort
</span><span class="code-line">puts a
</span><span class="code-line"><span class="token comment"># 我们没有修改对象</span>
</span><span class="code-line"><span class="token comment"># Drama</span>
</span><span class="code-line"><span class="token comment"># Mystery</span>
</span><span class="code-line"><span class="token comment"># Crime</span>
</span><span class="code-line"><span class="token comment"># Sci-fi</span>
</span><span class="code-line"><span class="token comment"># Disaster</span>
</span><span class="code-line"><span class="token comment"># Thriller</span>
</span><span class="code-line">a<span class="token punctuation">.</span>sort<span class="token operator">!</span>
</span><span class="code-line">puts a
</span><span class="code-line"><span class="token comment"># 修改对象</span>
</span><span class="code-line"><span class="token comment"># Crime</span>
</span><span class="code-line"><span class="token comment"># Disaster</span>
</span><span class="code-line"><span class="token comment"># Drama</span>
</span><span class="code-line"><span class="token comment"># Mystery</span>
</span><span class="code-line"><span class="token comment"># Sci-fi</span>
</span><span class="code-line"><span class="token comment"># Thriller</span>
</span></code></pre>
<p>当您要修改对象时，在方法之后使用 <code>!</code></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="布尔方法"><a aria-hidden="true" tabindex="-1" href="#布尔方法"><span class="icon icon-link"></span></a>布尔方法</h3><div class="wrap-body">
<p>在 ruby 中，以问号 (?) 结尾的方法称为布尔方法，它返回 true 或 false</p>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token string-literal"><span class="token string">"some text"</span></span><span class="token punctuation">.</span><span class="token keyword">nil</span><span class="token operator">?</span>
</span><span class="code-line"><span class="token comment"># false</span>
</span><span class="code-line"><span class="token keyword">nil</span><span class="token punctuation">.</span><span class="token keyword">nil</span><span class="token operator">?</span>
</span><span class="code-line"><span class="token comment"># true</span>
</span></code></pre>
<p>您可以拥有自己的布尔方法</p>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">is_vowel</span></span><span class="token operator">?</span><span class="token punctuation">(</span>char<span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">'a'</span></span><span class="token punctuation">,</span><span class="token string-literal"><span class="token string">'e'</span></span><span class="token punctuation">,</span><span class="token string-literal"><span class="token string">'i'</span></span><span class="token punctuation">,</span><span class="token string-literal"><span class="token string">'o'</span></span><span class="token punctuation">,</span><span class="token string-literal"><span class="token string">'u'</span></span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token keyword">include</span><span class="token operator">?</span> char
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">is_vowel<span class="token operator">?</span> <span class="token string-literal"><span class="token string">'a'</span></span>
</span><span class="code-line"><span class="token comment"># true</span>
</span><span class="code-line">is_vowel<span class="token operator">?</span> <span class="token string-literal"><span class="token string">'b'</span></span>
</span><span class="code-line"><span class="token comment"># false</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="blocks-块"><a aria-hidden="true" tabindex="-1" href="#blocks-块"><span class="icon icon-link"></span></a>Blocks (块)</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="块示例"><a aria-hidden="true" tabindex="-1" href="#块示例"><span class="icon icon-link"></span></a>块示例</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token comment"># return value</span>
</span><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">give_me_data</span></span>
</span><span class="code-line">    data <span class="token operator">=</span> <span class="token keyword">yield</span>
</span><span class="code-line">    puts <span class="token string-literal"><span class="token string">"data = </span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">data</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">give_me_data <span class="token punctuation">{</span> <span class="token string-literal"><span class="token string">"Big data"</span></span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出 => data = Big data</span>
</span></code></pre>
<p><code>do</code> 和 <code>end</code>（用于多行）或花括号 <code>{</code> 和 <code>}</code>（用于单行）之间的代码称为块，它们可以在两个管道之间定义多个参数 <code>(|arg1, arg2|)</code></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="单行块"><a aria-hidden="true" tabindex="-1" href="#单行块"><span class="icon icon-link"></span></a>单行块</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">salary <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">399</span><span class="token punctuation">,</span> <span class="token number">234</span><span class="token punctuation">,</span> <span class="token number">566</span><span class="token punctuation">,</span> <span class="token number">533</span><span class="token punctuation">,</span> <span class="token number">233</span><span class="token punctuation">]</span>
</span><span class="code-line">salary<span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token punctuation">{</span> <span class="token operator">|</span>s<span class="token operator">|</span> puts s <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># puts s = block body</span>
</span><span class="code-line"><span class="token comment"># |s| = block arugments</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="多行块"><a aria-hidden="true" tabindex="-1" href="#多行块"><span class="icon icon-link"></span></a>多行块</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">salary<span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token keyword">do</span> <span class="token operator">|</span>s<span class="token operator">|</span>
</span><span class="code-line">    a <span class="token operator">=</span> <span class="token number">10</span>
</span><span class="code-line">    res <span class="token operator">=</span> a <span class="token operator">*</span> s
</span><span class="code-line">    puts res
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line"><span class="token comment"># 块体</span>
</span><span class="code-line"><span class="token comment"># a = 10</span>
</span><span class="code-line"><span class="token comment"># res = a * s</span>
</span><span class="code-line"><span class="token comment"># puts res</span>
</span><span class="code-line"><span class="token comment"># 块参数</span>
</span><span class="code-line"><span class="token comment"># |s|</span>
</span></code></pre>
<p>块可以作为方法参数传递，也可以与方法调用相关联。 块返回最后评估的语句</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="隐式传递一个块"><a aria-hidden="true" tabindex="-1" href="#隐式传递一个块"><span class="icon icon-link"></span></a>隐式传递一个块</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">give_me_data</span></span>
</span><span class="code-line">    puts <span class="token string-literal"><span class="token string">"I am inside give_me_data method"</span></span>
</span><span class="code-line">    <span class="token keyword">yield</span>
</span><span class="code-line">    puts <span class="token string-literal"><span class="token string">"I am back in give_me_data method"</span></span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">give_me_data <span class="token punctuation">{</span> puts <span class="token string-literal"><span class="token string">"Big data"</span></span> <span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># I am inside give_me_data method</span>
</span><span class="code-line"><span class="token comment"># Big data</span>
</span><span class="code-line"><span class="token comment"># I am back in give_me_data method</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="多次调用"><a aria-hidden="true" tabindex="-1" href="#多次调用"><span class="icon icon-link"></span></a>多次调用</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">give_me_data</span></span>
</span><span class="code-line">    <span class="token keyword">yield</span>
</span><span class="code-line">    <span class="token keyword">yield</span>
</span><span class="code-line">    <span class="token keyword">yield</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">give_me_data <span class="token punctuation">{</span> puts <span class="token string-literal"><span class="token string">"Big data"</span></span> <span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># Big data</span>
</span><span class="code-line"><span class="token comment"># Big data</span>
</span><span class="code-line"><span class="token comment"># Big data</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用块参数调用"><a aria-hidden="true" tabindex="-1" href="#使用块参数调用"><span class="icon icon-link"></span></a>使用块参数调用</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">give_me_data</span></span>
</span><span class="code-line">    <span class="token keyword">yield</span> <span class="token number">10</span>
</span><span class="code-line">    <span class="token keyword">yield</span> <span class="token number">100</span>
</span><span class="code-line">    <span class="token keyword">yield</span> <span class="token number">30</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">give_me_data <span class="token punctuation">{</span> <span class="token operator">|</span>data<span class="token operator">|</span> puts <span class="token string-literal"><span class="token string">"Big data </span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">data</span><span class="token delimiter punctuation">}</span></span><span class="token string"> TB"</span></span> <span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># Big data 10 TB</span>
</span><span class="code-line"><span class="token comment"># Big data 100 TB</span>
</span><span class="code-line"><span class="token comment"># Big data 30 TB</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用多个块参数调用"><a aria-hidden="true" tabindex="-1" href="#使用多个块参数调用"><span class="icon icon-link"></span></a>使用多个块参数调用</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">give_me_data</span></span>
</span><span class="code-line">    <span class="token keyword">yield</span> <span class="token string-literal"><span class="token string">"Big data"</span></span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"TB"</span></span>
</span><span class="code-line">    <span class="token keyword">yield</span> <span class="token string-literal"><span class="token string">"Big data"</span></span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"GB"</span></span>
</span><span class="code-line">    <span class="token keyword">yield</span> <span class="token string-literal"><span class="token string">"Big data"</span></span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"MB"</span></span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">give_me_data <span class="token punctuation">{</span> <span class="token operator">|</span>text<span class="token punctuation">,</span> data<span class="token punctuation">,</span> unit<span class="token operator">|</span> puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">text</span><span class="token delimiter punctuation">}</span></span><span class="token string"> </span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">data</span><span class="token delimiter punctuation">}</span></span><span class="token string"> </span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">unit</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span> <span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># Big data 10 TB</span>
</span><span class="code-line"><span class="token comment"># Big data 100 GB</span>
</span><span class="code-line"><span class="token comment"># Big data 30 MB</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="块将尝试从当前上下文返回"><a aria-hidden="true" tabindex="-1" href="#块将尝试从当前上下文返回"><span class="icon icon-link"></span></a>块将尝试从当前上下文返回</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">give_me_data
</span><span class="code-line">    puts <span class="token string-literal"><span class="token string">"我在 give_me_data 方法里面"</span></span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">test</span></span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"我在测试方法里面"</span></span>
</span><span class="code-line">  give_me_data <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token number">10</span> <span class="token punctuation">}</span> <span class="token comment"># 代码从这里返回</span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"I am back in test method"</span></span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">return_value <span class="token operator">=</span> test
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 我在测试方法里面</span>
</span><span class="code-line"><span class="token comment"># 我在 give_me_data 方法里面</span>
</span><span class="code-line"><span class="token comment"># 10</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="通过使用--参数显式传递块"><a aria-hidden="true" tabindex="-1" href="#通过使用--参数显式传递块"><span class="icon icon-link"></span></a>通过使用 &#x26; 参数显式传递块</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">give_me_data</span></span><span class="token punctuation">(</span><span class="token operator">&#x26;</span>block<span class="token punctuation">)</span>
</span><span class="code-line">    block<span class="token punctuation">.</span>call
</span><span class="code-line">    block<span class="token punctuation">.</span>call
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">give_me_data <span class="token punctuation">{</span> puts <span class="token string-literal"><span class="token string">"Big data"</span></span> <span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># Big data</span>
</span><span class="code-line"><span class="token comment"># Big data</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="检查是否给出了块"><a aria-hidden="true" tabindex="-1" href="#检查是否给出了块"><span class="icon icon-link"></span></a>检查是否给出了块</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">give_me_data</span></span>
</span><span class="code-line">    <span class="token keyword">yield</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">give_me_data
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># LocalJumpError: no block given (yield)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="处理异常并使块可选的方法"><a aria-hidden="true" tabindex="-1" href="#处理异常并使块可选的方法"><span class="icon icon-link"></span></a>处理异常并使块可选的方法</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">give_me_data</span></span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string-literal"><span class="token string">"no block"</span></span> <span class="token keyword">unless</span> block_given<span class="token operator">?</span>
</span><span class="code-line">    <span class="token keyword">yield</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">give_me_data <span class="token punctuation">{</span> puts <span class="token string-literal"><span class="token string">"Big data"</span></span> <span class="token punctuation">}</span>
</span><span class="code-line">give_me_data
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># Big data</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="procs"><a aria-hidden="true" tabindex="-1" href="#procs"><span class="icon icon-link"></span></a>Procs</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="procs-示例"><a aria-hidden="true" tabindex="-1" href="#procs-示例"><span class="icon icon-link"></span></a>Procs 示例</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">p <span class="token operator">=</span> <span class="token class-name">Proc</span><span class="token punctuation">.</span><span class="token keyword">new</span> <span class="token punctuation">{</span> puts <span class="token string-literal"><span class="token string">"Hello World"</span></span> <span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">give_me_data</span></span><span class="token punctuation">(</span>proc<span class="token punctuation">)</span>
</span><span class="code-line">    proc<span class="token punctuation">.</span>call
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">give_me_data p
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># Hello World</span>
</span></code></pre>
<p>proc 就像一个可以存储在变量中的块</p>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="任意参数"><a aria-hidden="true" tabindex="-1" href="#任意参数"><span class="icon icon-link"></span></a>任意参数</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">p <span class="token operator">=</span> <span class="token class-name">Proc</span><span class="token punctuation">.</span><span class="token keyword">new</span> <span class="token punctuation">{</span> <span class="token operator">|</span>count<span class="token operator">|</span> <span class="token string-literal"><span class="token string">"Hello World "</span></span> <span class="token operator">*</span> count <span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">give_me_data</span></span><span class="token punctuation">(</span>proc<span class="token punctuation">)</span>
</span><span class="code-line">    proc<span class="token punctuation">.</span>call <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">2</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">give_me_data p
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># "Hello World Hello World Hello World Hello World Hello World "</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="proc-将尝试从当前上下文返回"><a aria-hidden="true" tabindex="-1" href="#proc-将尝试从当前上下文返回"><span class="icon icon-link"></span></a>proc 将尝试从当前上下文返回</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">p <span class="token operator">=</span> <span class="token class-name">Proc</span><span class="token punctuation">.</span><span class="token keyword">new</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token number">10</span> <span class="token punctuation">}</span>
</span><span class="code-line">p<span class="token punctuation">.</span>call
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token symbol">LocalJumpError</span><span class="token operator">:</span> unexpected <span class="token keyword">return</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="不能从顶级上下文返回"><a aria-hidden="true" tabindex="-1" href="#不能从顶级上下文返回"><span class="icon icon-link"></span></a>不能从顶级上下文返回</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">give_me_data</span></span>
</span><span class="code-line">    puts <span class="token string-literal"><span class="token string">"我在 give_me_data 方法里面"</span></span>
</span><span class="code-line">    p <span class="token operator">=</span> <span class="token class-name">Proc</span><span class="token punctuation">.</span><span class="token keyword">new</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token number">10</span> <span class="token punctuation">}</span>
</span><span class="code-line">    p<span class="token punctuation">.</span>call <span class="token comment"># 代码从这里返回</span>
</span><span class="code-line">    puts <span class="token string-literal"><span class="token string">"I am back in give_me_data method"</span></span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">return_value <span class="token operator">=</span> give_me_data
</span><span class="code-line">puts return_value
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 我在 give_me_data 方法里面</span>
</span><span class="code-line"><span class="token comment"># 10</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="lambdas"><a aria-hidden="true" tabindex="-1" href="#lambdas"><span class="icon icon-link"></span></a>Lambdas</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="声明一个-lambda"><a aria-hidden="true" tabindex="-1" href="#声明一个-lambda"><span class="icon icon-link"></span></a>声明一个 lambda</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">l <span class="token operator">=</span> lambda <span class="token punctuation">{</span> puts <span class="token string-literal"><span class="token string">"Hello World"</span></span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 速记</span>
</span><span class="code-line">l <span class="token operator">=</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span> puts <span class="token string-literal"><span class="token string">"Hello World"</span></span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 调用 lambda</span>
</span><span class="code-line">l<span class="token punctuation">.</span>call
</span><span class="code-line"><span class="token comment"># 输出 => Hello World</span>
</span></code></pre>
<p>有多种方法可以调用 lambda</p>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">l<span class="token punctuation">.</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">l<span class="token punctuation">[</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="严格的-arguments"><a aria-hidden="true" tabindex="-1" href="#严格的-arguments"><span class="icon icon-link"></span></a>严格的 arguments</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">l <span class="token operator">=</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">(</span>count<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token string-literal"><span class="token string">"Hello World "</span></span> <span class="token operator">*</span> count <span class="token punctuation">}</span>
</span><span class="code-line">l<span class="token punctuation">.</span>call <span class="token number">5</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># "Hello World Hello World Hello World Hello World Hello World "</span>
</span><span class="code-line">l<span class="token punctuation">.</span>call <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">2</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line">wrong number of arguments <span class="token punctuation">(</span>given <span class="token number">2</span><span class="token punctuation">,</span> expected <span class="token number">1</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="块中声明一个-lambda"><a aria-hidden="true" tabindex="-1" href="#块中声明一个-lambda"><span class="icon icon-link"></span></a>块中声明一个 lambda</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">def</span> <span class="token method-definition"><span class="token function">give_me_data</span></span>
</span><span class="code-line">    puts <span class="token string-literal"><span class="token string">"I am inside give_me_data method"</span></span>
</span><span class="code-line">    l <span class="token operator">=</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token number">10</span> <span class="token punctuation">}</span>
</span><span class="code-line">    l<span class="token punctuation">.</span>call
</span><span class="code-line">    puts <span class="token string-literal"><span class="token string">"I am back in give_me_data method"</span></span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">
</span><span class="code-line">return_value <span class="token operator">=</span> give_me_data
</span><span class="code-line">puts return_value
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># I am inside give_me_data method</span>
</span><span class="code-line"><span class="token comment"># I am back in give_me_data method</span>
</span><span class="code-line"><span class="token comment"># nil # because puts return nil</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="lambdas-从-lambda-本身返回就像常规方法一样"><a aria-hidden="true" tabindex="-1" href="#lambdas-从-lambda-本身返回就像常规方法一样"><span class="icon icon-link"></span></a>lambdas 从 lambda 本身返回，就像常规方法一样</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">l <span class="token operator">=</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token number">10</span> <span class="token punctuation">}</span>
</span><span class="code-line">l<span class="token punctuation">.</span>call
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 输出 => 10</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="数组"><a aria-hidden="true" tabindex="-1" href="#数组"><span class="icon icon-link"></span></a>数组</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="初始化一个空数组"><a aria-hidden="true" tabindex="-1" href="#初始化一个空数组"><span class="icon icon-link"></span></a>初始化一个空数组</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">array <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span><span class="token keyword">new</span>   <span class="token comment">#=> []</span>
</span><span class="code-line"><span class="token comment"># or</span>
</span><span class="code-line">array <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="包含不同类型的对象的数组"><a aria-hidden="true" tabindex="-1" href="#包含不同类型的对象的数组"><span class="icon icon-link"></span></a>包含不同类型的对象的数组</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">array <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"two"</span></span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">]</span> 
</span><span class="code-line"><span class="token comment">#=> [1, "two", 3.0]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="用初始大小和默认对象填充数组"><a aria-hidden="true" tabindex="-1" href="#用初始大小和默认对象填充数组"><span class="icon icon-link"></span></a>用初始大小和默认对象填充数组</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span><span class="token keyword">new</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>       
</span><span class="code-line"><span class="token comment">#=> [nil, nil, nil]</span>
</span><span class="code-line">numbers <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span><span class="token keyword">new</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">)</span>    
</span><span class="code-line"><span class="token comment">#=> [7, 7, 7]</span>
</span><span class="code-line">numbers <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span><span class="token keyword">new</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token comment">#=> [true, true, true]</span>
</span><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>fill<span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">0.</span><span class="token number">.2</span><span class="token punctuation">)</span>   <span class="token comment">#=> [7, 7, 7]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="不同哈希值的数组"><a aria-hidden="true" tabindex="-1" href="#不同哈希值的数组"><span class="icon icon-link"></span></a>不同哈希值的数组</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">array_with_hashes <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span><span class="token keyword">new</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment">#=> [{}, {}]</span>
</span><span class="code-line">array_with_hashes<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token symbol">:name</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Bob"</span></span>
</span><span class="code-line">array_with_hashes<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token symbol">:id</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">10</span>          <span class="token comment">#=> [{:name=>"Bob", :id=>10}, {}]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="二维数组"><a aria-hidden="true" tabindex="-1" href="#二维数组"><span class="icon icon-link"></span></a>二维数组</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">temperature_data <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">              <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"A908"</span></span><span class="token punctuation">,</span> <span class="token number">38</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
</span><span class="code-line">              <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"A909"</span></span><span class="token punctuation">,</span> <span class="token number">37</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
</span><span class="code-line">              <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"A910"</span></span><span class="token punctuation">,</span> <span class="token number">38</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
</span><span class="code-line">          <span class="token punctuation">]</span>
</span><span class="code-line">temperature_data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>    <span class="token comment">#=> ["A908", 38]</span>
</span><span class="code-line">temperature_data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token comment">#=> "A908"</span>
</span><span class="code-line">temperature_data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token comment">#=> 38</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="数组索引"><a aria-hidden="true" tabindex="-1" href="#数组索引"><span class="icon icon-link"></span></a>数组索引</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">str_array <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"This"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"is"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"a"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"small"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"array"</span></span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span><span class="code-line">str_array<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>            <span class="token comment">#=> "This"</span>
</span><span class="code-line">str_array<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span>            <span class="token comment">#=> "is"</span>
</span><span class="code-line">str_array<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span>            <span class="token comment">#=> "array"</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="负索引"><a aria-hidden="true" tabindex="-1" href="#负索引"><span class="icon icon-link"></span></a>负索引</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">str_array <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"This"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"is"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"a"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"small"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"array"</span></span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># 索引 -1 表示最后一个元素</span>
</span><span class="code-line">str_array<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>        <span class="token comment">#=> "array"</span>
</span><span class="code-line"><span class="token comment"># 索引 -2 表示倒数第二个元素</span>
</span><span class="code-line">str_array<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span>        <span class="token comment">#=> "small"</span>
</span><span class="code-line">str_array<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">6</span><span class="token punctuation">]</span>        <span class="token comment">#=> nil</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="数组方法-at"><a aria-hidden="true" tabindex="-1" href="#数组方法-at"><span class="icon icon-link"></span></a>数组方法 at</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">str_array <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"This"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"is"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"a"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"small"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"array"</span></span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line">puts str_array<span class="token punctuation">.</span>at<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>      <span class="token comment">#=> "This"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="范围获取"><a aria-hidden="true" tabindex="-1" href="#范围获取"><span class="icon icon-link"></span></a>范围获取</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span>
</span><span class="code-line">arr<span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">]</span>                  <span class="token comment">#=> nil</span>
</span><span class="code-line">arr<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">]</span>                   <span class="token comment">#=> 4</span>
</span><span class="code-line">arr<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>                 <span class="token comment">#=> [3, 4, 5]</span>
</span><span class="code-line">arr<span class="token punctuation">[</span><span class="token number">1.</span><span class="token number">.4</span><span class="token punctuation">]</span>                 <span class="token comment">#=> [2, 3, 4, 5]</span>
</span><span class="code-line">arr<span class="token punctuation">[</span><span class="token number">1.</span><span class="token punctuation">.</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">]</span>                <span class="token comment">#=> [2, 3, 4]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="数组方法-fetch"><a aria-hidden="true" tabindex="-1" href="#数组方法-fetch"><span class="icon icon-link"></span></a>数组方法 fetch</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">'a'</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">'b'</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">'c'</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">'d'</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">'e'</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">'f'</span></span><span class="token punctuation">]</span>
</span><span class="code-line">arr<span class="token punctuation">.</span>fetch<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">#=> IndexError: 数组边界外的索引 100：-6...6</span>
</span><span class="code-line">arr<span class="token punctuation">.</span>fetch<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"oops"</span></span><span class="token punctuation">)</span>    <span class="token comment">#=> "oops"</span>
</span></code></pre>
<p>超出边界，给默认值</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="获取数组元素"><a aria-hidden="true" tabindex="-1" href="#获取数组元素"><span class="icon icon-link"></span></a>获取数组元素</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line">arr<span class="token punctuation">.</span>first     <span class="token comment"># 第一个值 => 1</span>
</span><span class="code-line">arr<span class="token punctuation">.</span>last      <span class="token comment"># 最后一个值 => 6</span>
</span><span class="code-line"><span class="token comment"># take 返回前 n 个元素</span>
</span><span class="code-line">arr<span class="token punctuation">.</span>take<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>   <span class="token comment">#=> [1, 2, 3]</span>
</span><span class="code-line"><span class="token comment"># drop 在 n 个元素被删除之后</span>
</span><span class="code-line">arr<span class="token punctuation">.</span>drop<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>   <span class="token comment">#=> [4, 5, 6]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="在数组末尾添加值-push"><a aria-hidden="true" tabindex="-1" href="#在数组末尾添加值-push"><span class="icon icon-link"></span></a>在数组末尾添加值 push</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>push<span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span>          
</span><span class="code-line"><span class="token comment">#=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>push<span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">)</span>  
</span><span class="code-line"><span class="token comment">#=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="数组末尾删除值-pop"><a aria-hidden="true" tabindex="-1" href="#数组末尾删除值-pop"><span class="icon icon-link"></span></a>数组末尾删除值 pop</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">num_array <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span><span class="code-line">num_array<span class="token punctuation">.</span>pop             <span class="token comment">#=> 10</span>
</span><span class="code-line">num_array
</span><span class="code-line"><span class="token comment">#=> [1, 2, 3, 4, 5, 6, 7, 8, 9]</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="在数组的开头添加值-unshift"><a aria-hidden="true" tabindex="-1" href="#在数组的开头添加值-unshift"><span class="icon icon-link"></span></a>在数组的开头添加值 unshift</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>unshift<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>          
</span><span class="code-line"><span class="token comment">#=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>unshift<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token comment">#=> [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="检索并同时删除第一个元素-shift"><a aria-hidden="true" tabindex="-1" href="#检索并同时删除第一个元素-shift"><span class="icon icon-link"></span></a>检索并同时删除第一个元素 shift</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>shift <span class="token comment">#=> 1</span>
</span><span class="code-line">numbers
</span><span class="code-line"><span class="token comment">#=> [2, 3, 4, 5, 6, 7, 8, 9, 10]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="删除特定索引处的元素-delete_at"><a aria-hidden="true" tabindex="-1" href="#删除特定索引处的元素-delete_at"><span class="icon icon-link"></span></a>删除特定索引处的元素 delete_at</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>delete_at<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">#=> 4</span>
</span><span class="code-line">numbers             
</span><span class="code-line"><span class="token comment">#=> [2, 3, 5, 6, 7, 8, 9, 10]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="删除数组中任意位置的特定元素"><a aria-hidden="true" tabindex="-1" href="#删除数组中任意位置的特定元素"><span class="icon icon-link"></span></a>删除数组中任意位置的特定元素</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>delete<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">#=> 2</span>
</span><span class="code-line">numbers           <span class="token comment">#=> [3, 5, 6, 7, 8, 9, 10]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="在给定索引处插入值-insert"><a aria-hidden="true" tabindex="-1" href="#在给定索引处插入值-insert"><span class="icon icon-link"></span></a>在给定索引处插入值 insert</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>           
</span><span class="code-line"><span class="token comment">#=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>  
</span><span class="code-line"><span class="token comment">#=> [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span>
</span><span class="code-line">
</span><span class="code-line">numbers<span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">13</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token comment">#=> [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>insert<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span>         
</span><span class="code-line"><span class="token comment">#=> [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="一个块来填充数组的值"><a aria-hidden="true" tabindex="-1" href="#一个块来填充数组的值"><span class="icon icon-link"></span></a>一个块来填充数组的值</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span><span class="token keyword">new</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">|</span>n<span class="token operator">|</span> n <span class="token operator">=</span> n <span class="token operator">*</span> <span class="token number">2</span> <span class="token punctuation">}</span> 
</span><span class="code-line"><span class="token comment">#=> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="填充数组变得更容易"><a aria-hidden="true" tabindex="-1" href="#填充数组变得更容易"><span class="icon icon-link"></span></a>填充数组变得更容易</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token builtin">Array</span><span class="token punctuation">(</span><span class="token number">100.</span><span class="token number">.110</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">#=> [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># 或者我们可以将范围转换为数组</span>
</span><span class="code-line"><span class="token punctuation">(</span><span class="token number">100.</span><span class="token number">.110</span><span class="token punctuation">)</span><span class="token punctuation">.</span>to_a 
</span><span class="code-line"><span class="token comment">#=> [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110]</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="从数组中删除-nil-值"><a aria-hidden="true" tabindex="-1" href="#从数组中删除-nil-值"><span class="icon icon-link"></span></a>从数组中删除 nil 值</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">'foo'</span></span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">nil</span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">'bar'</span></span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token keyword">nil</span><span class="token punctuation">]</span>
</span><span class="code-line">arr<span class="token punctuation">.</span>compact  <span class="token comment">#=> ['foo', 0, 'bar', 7]</span>
</span><span class="code-line">arr      <span class="token comment">#=> ['foo', 0, nil, 'bar', 7, nil]</span>
</span><span class="code-line">arr<span class="token punctuation">.</span>compact<span class="token operator">!</span> <span class="token comment">#=> ['foo', 0, 'bar', 7]</span>
</span><span class="code-line">arr      <span class="token comment">#=> ['foo', 0, 'bar', 7]</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="去重-uniq"><a aria-hidden="true" tabindex="-1" href="#去重-uniq"><span class="icon icon-link"></span></a>去重 uniq</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">556</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">123</span><span class="token punctuation">,</span> <span class="token number">556</span><span class="token punctuation">]</span>
</span><span class="code-line">arr<span class="token punctuation">.</span>uniq <span class="token comment">#=> [2, 5, 6, 556, 8, 9, 0, 123]</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="检查数组中是否存在值include"><a aria-hidden="true" tabindex="-1" href="#检查数组中是否存在值include"><span class="icon icon-link"></span></a>检查数组中是否存在值（<code>include？</code>）</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">planets <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Mercury"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Venus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Earth"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Mars"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Jupiter"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Saturn"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Uranus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Neptune"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">planets<span class="token punctuation">.</span><span class="token keyword">include</span><span class="token operator">?</span> <span class="token string-literal"><span class="token string">"Mars"</span></span>
</span><span class="code-line"><span class="token comment"># 输出 => true</span>
</span><span class="code-line">planets<span class="token punctuation">.</span><span class="token keyword">include</span><span class="token operator">?</span> <span class="token string-literal"><span class="token string">"Pluto"</span></span>
</span><span class="code-line"><span class="token comment"># 输出 => false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="获取数组大小"><a aria-hidden="true" tabindex="-1" href="#获取数组大小"><span class="icon icon-link"></span></a>获取数组大小</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">planets <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Mercury"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Venus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Earth"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Mars"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Jupiter"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Saturn"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Uranus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Neptune"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">planets<span class="token punctuation">.</span>size
</span><span class="code-line"><span class="token comment"># 输出 => 8</span>
</span><span class="code-line">planets<span class="token punctuation">.</span>length
</span><span class="code-line"><span class="token comment"># 输出 => 8</span>
</span></code></pre>
<p>您可以使用大小或长度，两者都是同义词</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="清除数组"><a aria-hidden="true" tabindex="-1" href="#清除数组"><span class="icon icon-link"></span></a>清除数组</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>clear
</span><span class="code-line"><span class="token comment"># 输出 => []</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="获取数组的第一个元素"><a aria-hidden="true" tabindex="-1" href="#获取数组的第一个元素"><span class="icon icon-link"></span></a>获取数组的第一个元素</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># or</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>first
</span><span class="code-line"><span class="token comment"># 输出 => 1</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="获取数组的最后一个元素"><a aria-hidden="true" tabindex="-1" href="#获取数组的最后一个元素"><span class="icon icon-link"></span></a>获取数组的最后一个元素</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">[</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># or</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>last
</span><span class="code-line"><span class="token comment"># 输出 => 10</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="合并两个数组"><a aria-hidden="true" tabindex="-1" href="#合并两个数组"><span class="icon icon-link"></span></a>合并两个数组</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"tom"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"mot"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"otm"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">b <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line">a<span class="token punctuation">.</span>zip<span class="token punctuation">(</span>b<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># [["tom", 2], ["mot", 3], ["otm", 5]]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="对数组进行排序"><a aria-hidden="true" tabindex="-1" href="#对数组进行排序"><span class="icon icon-link"></span></a>对数组进行排序</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">primes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line">sorted_primes <span class="token operator">=</span> primes<span class="token punctuation">.</span>sort
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">sorted_primes</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line"><span class="token comment"># 输出 => [2, 3, 5, 7]</span>
</span></code></pre>
<p>or in-place sort</p>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">primes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line">primes<span class="token punctuation">.</span>sort<span class="token operator">!</span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">primes</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line"><span class="token comment"># 输出 => [2, 3, 5, 7]</span>
</span></code></pre>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">planets <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Mercury"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Venus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Earth"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Mars"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Jupiter"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Saturn"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Uranus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Neptune"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">planets<span class="token punctuation">.</span>sort
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># ["Earth", "Jupiter", "Mars", "Mercury", "Neptune", "Saturn", "Uranus", "Venus"]</span>
</span><span class="code-line">planets<span class="token punctuation">.</span>sort_by <span class="token punctuation">{</span> <span class="token operator">|</span>p<span class="token operator">|</span> p <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># ["Earth", "Jupiter", "Mars", "Mercury", "Neptune", "Saturn", "Uranus", "Venus"]</span>
</span><span class="code-line">planets<span class="token punctuation">.</span>sort_by <span class="token punctuation">{</span> <span class="token operator">|</span>p<span class="token operator">|</span> p<span class="token punctuation">.</span>length <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># ["Mars", "Earth", "Venus", "Saturn", "Uranus", "Neptune", "Jupiter", "Mercury"]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="从数组中获取最大值"><a aria-hidden="true" tabindex="-1" href="#从数组中获取最大值"><span class="icon icon-link"></span></a>从数组中获取最大值</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">primes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line">primes<span class="token punctuation">.</span>max_by <span class="token punctuation">{</span> <span class="token operator">|</span>p<span class="token operator">|</span> p <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出 => 7</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="使用范围获取数组元素"><a aria-hidden="true" tabindex="-1" href="#使用范围获取数组元素"><span class="icon icon-link"></span></a>使用范围获取数组元素</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token comment"># numbers[start..end], both index are inclusive</span>
</span><span class="code-line">puts numbers<span class="token punctuation">[</span><span class="token number">0.</span><span class="token number">.3</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># 1</span>
</span><span class="code-line"><span class="token comment"># 2</span>
</span><span class="code-line"><span class="token comment"># 3</span>
</span><span class="code-line"><span class="token comment"># 4</span>
</span><span class="code-line"><span class="token comment"># numbers[start..end], end index is exclusive</span>
</span><span class="code-line">puts numbers<span class="token punctuation">[</span><span class="token number">0.</span><span class="token punctuation">.</span><span class="token number">.3</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># 1</span>
</span><span class="code-line"><span class="token comment"># 2</span>
</span><span class="code-line"><span class="token comment"># 3</span>
</span><span class="code-line"><span class="token comment"># or numbers[start..length]</span>
</span><span class="code-line">puts numbers<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment"># 1</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="获取数组的前n个元素"><a aria-hidden="true" tabindex="-1" href="#获取数组的前n个元素"><span class="icon icon-link"></span></a>获取数组的前n个元素</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">primes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line">primes<span class="token punctuation">.</span>take<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># [7, 2, 3]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="访问元素"><a aria-hidden="true" tabindex="-1" href="#访问元素"><span class="icon icon-link"></span></a>访问元素</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">primes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line">primes<span class="token punctuation">.</span>fetch<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># 5</span>
</span><span class="code-line"><span class="token comment"># Fetch will throw an error if the element does not exist</span>
</span><span class="code-line">primes<span class="token punctuation">.</span>fetch<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># (index 10 outside of array bounds: -4...4)</span>
</span><span class="code-line"><span class="token comment"># or get an default value</span>
</span><span class="code-line">primes<span class="token punctuation">.</span>fetch<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># -1</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="从数组中删除重复元素"><a aria-hidden="true" tabindex="-1" href="#从数组中删除重复元素"><span class="icon icon-link"></span></a>从数组中删除重复元素</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>uniq
</span><span class="code-line"><span class="token comment"># [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="删除前-n-个元素"><a aria-hidden="true" tabindex="-1" href="#删除前-n-个元素"><span class="icon icon-link"></span></a>删除前 n 个元素</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">primes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line">primes<span class="token punctuation">.</span>drop<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># [5]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="删除第一个元素"><a aria-hidden="true" tabindex="-1" href="#删除第一个元素"><span class="icon icon-link"></span></a>删除第一个元素</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">primes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line">primes<span class="token punctuation">.</span>shift
</span><span class="code-line"><span class="token comment"># [2, 3, 5]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="删除最后一个元素"><a aria-hidden="true" tabindex="-1" href="#删除最后一个元素"><span class="icon icon-link"></span></a>删除最后一个元素</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">primes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line">primes<span class="token punctuation">.</span>pop
</span><span class="code-line"><span class="token comment"># [7, 2, 3]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="删除带有索引的元素"><a aria-hidden="true" tabindex="-1" href="#删除带有索引的元素"><span class="icon icon-link"></span></a>删除带有索引的元素</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">primes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line">primes<span class="token punctuation">.</span>delete_at<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># [7, 2, 3]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="删除所有出现的元素"><a aria-hidden="true" tabindex="-1" href="#删除所有出现的元素"><span class="icon icon-link"></span></a>删除所有出现的元素</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">primes <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line">primes<span class="token punctuation">.</span>delete<span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># [7, 2, 3]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="each"><a aria-hidden="true" tabindex="-1" href="#each"><span class="icon icon-link"></span></a>each</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token comment"># 当你有单行块时</span>
</span><span class="code-line">salary <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">399</span><span class="token punctuation">,</span> <span class="token number">234</span><span class="token punctuation">,</span> <span class="token number">566</span><span class="token punctuation">,</span> <span class="token number">533</span><span class="token punctuation">,</span> <span class="token number">233</span><span class="token punctuation">]</span>
</span><span class="code-line">salary<span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token punctuation">{</span> <span class="token operator">|</span>s<span class="token operator">|</span> puts s <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 399</span>
</span><span class="code-line"><span class="token comment"># 234</span>
</span><span class="code-line"><span class="token comment"># 566</span>
</span><span class="code-line"><span class="token comment"># 533</span>
</span><span class="code-line"><span class="token comment"># 233</span>
</span></code></pre>
<p>当你有一个多行块时，你可以用 <code>do</code> 和 <code>end</code> 替换花括号 <code>{}</code></p>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">salary<span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token keyword">do</span> <span class="token operator">|</span>s<span class="token operator">|</span>
</span><span class="code-line">  a <span class="token operator">=</span> <span class="token number">10</span>
</span><span class="code-line">  res <span class="token operator">=</span> a <span class="token operator">*</span> s
</span><span class="code-line">  puts res
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 3990</span>
</span><span class="code-line"><span class="token comment"># 2340</span>
</span><span class="code-line"><span class="token comment"># 5660</span>
</span><span class="code-line"><span class="token comment"># 5330</span>
</span><span class="code-line"><span class="token comment"># 2330</span>
</span></code></pre>
<p>或者您可以使用大括号 {} 和分号作为分隔符而不是换行符来做同样的事情</p>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">salary<span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token punctuation">{</span> <span class="token operator">|</span>s<span class="token operator">|</span> a <span class="token operator">=</span> <span class="token number">10</span> <span class="token punctuation">;</span> res <span class="token operator">=</span> a <span class="token operator">*</span> s <span class="token punctuation">;</span> puts res <span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="each_with_index"><a aria-hidden="true" tabindex="-1" href="#each_with_index"><span class="icon icon-link"></span></a>each_with_index</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">salary <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">399</span><span class="token punctuation">,</span> <span class="token number">234</span><span class="token punctuation">,</span> <span class="token number">566</span><span class="token punctuation">,</span> <span class="token number">533</span><span class="token punctuation">,</span> <span class="token number">233</span><span class="token punctuation">]</span>
</span><span class="code-line">salary<span class="token punctuation">.</span>each_with_index <span class="token punctuation">{</span> <span class="token operator">|</span>value<span class="token punctuation">,</span> index<span class="token operator">|</span> puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">index</span><span class="token delimiter punctuation">}</span></span><span class="token string"> </span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">value</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 0 399</span>
</span><span class="code-line"><span class="token comment"># 1 234</span>
</span><span class="code-line"><span class="token comment"># 2 566</span>
</span><span class="code-line"><span class="token comment"># 3 533</span>
</span><span class="code-line"><span class="token comment"># 4 233</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="each_index"><a aria-hidden="true" tabindex="-1" href="#each_index"><span class="icon icon-link"></span></a>each_index</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">salary <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">399</span><span class="token punctuation">,</span> <span class="token number">234</span><span class="token punctuation">,</span> <span class="token number">566</span><span class="token punctuation">,</span> <span class="token number">533</span><span class="token punctuation">,</span> <span class="token number">233</span><span class="token punctuation">]</span>
</span><span class="code-line">salary<span class="token punctuation">.</span>each_index <span class="token punctuation">{</span> <span class="token operator">|</span>i<span class="token operator">|</span> puts i<span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 0</span>
</span><span class="code-line"><span class="token comment"># 1</span>
</span><span class="code-line"><span class="token comment"># 2</span>
</span><span class="code-line"><span class="token comment"># 3</span>
</span><span class="code-line"><span class="token comment"># 4</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="map"><a aria-hidden="true" tabindex="-1" href="#map"><span class="icon icon-link"></span></a>map</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">salary <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">399</span><span class="token punctuation">,</span> <span class="token number">234</span><span class="token punctuation">,</span> <span class="token number">566</span><span class="token punctuation">,</span> <span class="token number">533</span><span class="token punctuation">,</span> <span class="token number">233</span><span class="token punctuation">]</span>
</span><span class="code-line">salary<span class="token punctuation">.</span>map <span class="token punctuation">{</span> <span class="token operator">|</span>s<span class="token operator">|</span>  s <span class="token operator">*</span> <span class="token number">10</span>  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 返回</span>
</span><span class="code-line"><span class="token comment"># [3990, 2340, 5660, 5330, 2330]</span>
</span><span class="code-line"><span class="token comment"># 另一方面，每个都返回原始值</span>
</span><span class="code-line">salary <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">399</span><span class="token punctuation">,</span> <span class="token number">234</span><span class="token punctuation">,</span> <span class="token number">566</span><span class="token punctuation">,</span> <span class="token number">533</span><span class="token punctuation">,</span> <span class="token number">233</span><span class="token punctuation">]</span>
</span><span class="code-line">salary<span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token punctuation">{</span> <span class="token operator">|</span>s<span class="token operator">|</span>  s <span class="token operator">*</span> <span class="token number">10</span>  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 返回</span>
</span><span class="code-line"><span class="token comment"># [399, 234, 566, 533, 233]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="collect"><a aria-hidden="true" tabindex="-1" href="#collect"><span class="icon icon-link"></span></a>collect</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">salary <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">399</span><span class="token punctuation">,</span> <span class="token number">234</span><span class="token punctuation">,</span> <span class="token number">566</span><span class="token punctuation">,</span> <span class="token number">533</span><span class="token punctuation">,</span> <span class="token number">233</span><span class="token punctuation">]</span>
</span><span class="code-line">salary<span class="token punctuation">.</span>collect <span class="token punctuation">{</span> <span class="token operator">|</span>s<span class="token operator">|</span> s <span class="token operator">></span> <span class="token number">400</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># [false, false, true, true, false]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="for"><a aria-hidden="true" tabindex="-1" href="#for"><span class="icon icon-link"></span></a>for</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">for</span> value <span class="token keyword">in</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>
</span><span class="code-line">    puts value
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="each_with_object"><a aria-hidden="true" tabindex="-1" href="#each_with_object"><span class="icon icon-link"></span></a>each_with_object</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">colors <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token punctuation">{</span><span class="token symbol">color</span><span class="token operator">:</span> <span class="token string-literal"><span class="token string">"red"</span></span><span class="token punctuation">,</span> <span class="token symbol">count</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token symbol">color</span><span class="token operator">:</span> <span class="token string-literal"><span class="token string">"red"</span></span><span class="token punctuation">,</span> <span class="token symbol">count</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token symbol">color</span><span class="token operator">:</span> <span class="token string-literal"><span class="token string">"black"</span></span><span class="token punctuation">,</span> <span class="token symbol">count</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span><span class="code-line">colors<span class="token punctuation">.</span>each_with_object<span class="token punctuation">(</span><span class="token class-name">Hash</span><span class="token punctuation">.</span><span class="token keyword">new</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">|</span>color<span class="token punctuation">,</span> hash<span class="token operator">|</span> hash<span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"color_"</span></span><span class="token operator">+</span>color<span class="token punctuation">[</span><span class="token symbol">:color</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> color<span class="token punctuation">[</span><span class="token symbol">:color</span><span class="token punctuation">]</span><span class="token punctuation">.</span>upcase<span class="token punctuation">;</span> hash<span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"count_"</span></span><span class="token operator">+</span>color<span class="token punctuation">[</span><span class="token symbol">:color</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">+=</span> color<span class="token punctuation">[</span><span class="token symbol">:count</span><span class="token punctuation">]</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token punctuation">{</span><span class="token string-literal"><span class="token string">"color_red"</span></span><span class="token operator">=></span><span class="token string-literal"><span class="token string">"RED"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"count_red"</span></span><span class="token operator">=></span><span class="token number">8</span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"color_black"</span></span><span class="token operator">=></span><span class="token string-literal"><span class="token string">"BLACK"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"count_black"</span></span><span class="token operator">=></span><span class="token number">4</span><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span>each_with_object<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">|</span>number<span class="token punctuation">,</span> sum<span class="token operator">|</span> sum <span class="token operator">+=</span> number<span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 0</span>
</span><span class="code-line"><span class="token comment"># 因为0是不可变的，由于初始对象是0，所以方法返回0</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="while"><a aria-hidden="true" tabindex="-1" href="#while"><span class="icon icon-link"></span></a>while</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">planets <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Mercury"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Venus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Earth"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Mars"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Jupiter"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Saturn"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Uranus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Neptune"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">index <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line"><span class="token keyword">while</span> index <span class="token operator">&#x3C;</span> planets<span class="token punctuation">.</span>size
</span><span class="code-line">    puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">planets<span class="token punctuation">[</span>index<span class="token punctuation">]</span></span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line">    index <span class="token operator">+=</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
<hr>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">a <span class="token operator">=</span> <span class="token number">1</span>
</span><span class="code-line">star <span class="token operator">=</span> <span class="token string-literal"><span class="token string">'*'</span></span>
</span><span class="code-line"><span class="token keyword">while</span> a <span class="token operator">&#x3C;=</span> <span class="token number">10</span>
</span><span class="code-line">    puts star
</span><span class="code-line">    star <span class="token operator">+=</span> <span class="token string-literal"><span class="token string">'*'</span></span>
</span><span class="code-line">    a <span class="token operator">+=</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="do-while"><a aria-hidden="true" tabindex="-1" href="#do-while"><span class="icon icon-link"></span></a>do while</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">planets <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Mercury"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Venus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Earth"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Mars"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Jupiter"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Saturn"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Uranus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Neptune"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">index <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line">loop <span class="token keyword">do</span>
</span><span class="code-line">    puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">planets<span class="token punctuation">[</span>index<span class="token punctuation">]</span></span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line">    index <span class="token operator">+=</span> <span class="token number">1</span>
</span><span class="code-line">    <span class="token keyword">break</span> <span class="token keyword">if</span> planets<span class="token punctuation">[</span>index<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string-literal"><span class="token string">"Mars"</span></span> <span class="token keyword">or</span> index <span class="token operator">></span> planets<span class="token punctuation">.</span>size
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="until"><a aria-hidden="true" tabindex="-1" href="#until"><span class="icon icon-link"></span></a>until</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">planets <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Mercury"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Venus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Earth"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Mars"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Jupiter"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Saturn"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Uranus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Neptune"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">index <span class="token operator">=</span> planets<span class="token punctuation">.</span>size <span class="token operator">-</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token keyword">until</span> index <span class="token operator">&#x3C;</span> <span class="token number">0</span>
</span><span class="code-line">    puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">planets<span class="token punctuation">[</span>index<span class="token punctuation">]</span></span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line">    index <span class="token operator">-=</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">a <span class="token operator">=</span> <span class="token number">1</span>
</span><span class="code-line">star <span class="token operator">=</span> <span class="token string-literal"><span class="token string">'*'</span></span>
</span><span class="code-line"><span class="token keyword">until</span> star<span class="token punctuation">.</span>length <span class="token operator">></span> <span class="token number">10</span>
</span><span class="code-line">    puts star
</span><span class="code-line">    star <span class="token operator">+=</span> <span class="token string-literal"><span class="token string">'*'</span></span>
</span><span class="code-line">    a <span class="token operator">+=</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="times"><a aria-hidden="true" tabindex="-1" href="#times"><span class="icon icon-link"></span></a>times</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token number">10.</span>times <span class="token punctuation">{</span> puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">rand<span class="token punctuation">(</span><span class="token number">1.</span><span class="token number">.100</span><span class="token punctuation">)</span></span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 将打印 10 个随机数</span>
</span></code></pre>
<p>仅仅因为你可以并不意味着你应该像这样迭代一个数组</p>
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">data_sample <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>
</span><span class="code-line">data_sample<span class="token punctuation">.</span>size<span class="token punctuation">.</span>times <span class="token punctuation">{</span> <span class="token operator">|</span>index<span class="token operator">|</span> puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">data_sample<span class="token punctuation">[</span>index<span class="token punctuation">]</span></span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 2</span>
</span><span class="code-line"><span class="token comment"># 3</span>
</span><span class="code-line"><span class="token comment"># 5</span>
</span><span class="code-line"><span class="token comment"># 7</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="upto"><a aria-hidden="true" tabindex="-1" href="#upto"><span class="icon icon-link"></span></a>upto</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">data_sample <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token number">0.</span>upto<span class="token punctuation">(</span><span class="token punctuation">(</span>data_sample<span class="token punctuation">.</span>size <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">|</span>index<span class="token operator">|</span> puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">data_sample<span class="token punctuation">[</span>index<span class="token punctuation">]</span></span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 2</span>
</span><span class="code-line"><span class="token comment"># 3</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="downto"><a aria-hidden="true" tabindex="-1" href="#downto"><span class="icon icon-link"></span></a>downto</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">data_sample <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">(</span>data_sample<span class="token punctuation">.</span>size <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span>downto<span class="token punctuation">(</span>data_sample<span class="token punctuation">.</span>size <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">|</span>index<span class="token operator">|</span> puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">data_sample<span class="token punctuation">[</span>index<span class="token punctuation">]</span></span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 7</span>
</span><span class="code-line"><span class="token comment"># 5</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="step"><a aria-hidden="true" tabindex="-1" href="#step"><span class="icon icon-link"></span></a>step</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token number">1.</span>step<span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">|</span>number<span class="token operator">|</span> puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">number</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 1</span>
</span><span class="code-line"><span class="token comment"># 3</span>
</span><span class="code-line"><span class="token comment"># 5</span>
</span><span class="code-line"><span class="token comment"># 7</span>
</span><span class="code-line"><span class="token comment"># 9</span>
</span><span class="code-line"><span class="token comment"># 11</span>
</span><span class="code-line"><span class="token comment"># 13</span>
</span><span class="code-line"><span class="token comment"># 15</span>
</span><span class="code-line"><span class="token comment"># 17</span>
</span><span class="code-line"><span class="token comment"># 19</span>
</span></code></pre>
<hr>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token number">19.</span>step<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">|</span>number<span class="token operator">|</span> puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">number</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 19</span>
</span><span class="code-line"><span class="token comment"># 17</span>
</span><span class="code-line"><span class="token comment"># 15</span>
</span><span class="code-line"><span class="token comment"># 13</span>
</span><span class="code-line"><span class="token comment"># 11</span>
</span><span class="code-line"><span class="token comment"># 9</span>
</span><span class="code-line"><span class="token comment"># 7</span>
</span><span class="code-line"><span class="token comment"># 5</span>
</span><span class="code-line"><span class="token comment"># 3</span>
</span><span class="code-line"><span class="token comment"># 1</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="inject"><a aria-hidden="true" tabindex="-1" href="#inject"><span class="icon icon-link"></span></a>inject</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>inject<span class="token punctuation">{</span> <span class="token operator">|</span>res<span class="token punctuation">,</span> n<span class="token operator">|</span> res <span class="token operator">+</span> n <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 输出是所有数字之和的结果</span>
</span><span class="code-line"><span class="token comment"># 如果不给res设置初始值，则数组的第一个元素作为res的初始值</span>
</span><span class="code-line"><span class="token comment"># 10</span>
</span><span class="code-line"><span class="token comment"># 现在将 res 的值设置为 11</span>
</span><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>inject<span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">|</span>res<span class="token punctuation">,</span> n<span class="token operator">|</span> res <span class="token operator">+</span> n <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># so 11 + 2, 13 + 2, 15 + 2, 17 + 2 and 19 + 2</span>
</span><span class="code-line"><span class="token comment"># 21</span>
</span><span class="code-line"><span class="token comment"># using symbol</span>
</span><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>inject<span class="token punctuation">(</span><span class="token operator">:</span><span class="token operator">+</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 10</span>
</span></code></pre>
<p>使用初始值和符号</p>
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>inject<span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">,</span> <span class="token operator">:</span><span class="token operator">+</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 21</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="reduce"><a aria-hidden="true" tabindex="-1" href="#reduce"><span class="icon icon-link"></span></a>reduce</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>reduce<span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">,</span> <span class="token operator">:</span><span class="token operator">+</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 21</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="detect"><a aria-hidden="true" tabindex="-1" href="#detect"><span class="icon icon-link"></span></a>detect</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">planets <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Mercury"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Venus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Earth"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Mars"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Jupiter"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Saturn"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Uranus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Neptune"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">planets<span class="token punctuation">.</span>detect <span class="token punctuation">{</span> <span class="token operator">|</span>name<span class="token operator">|</span> name<span class="token punctuation">.</span>start_with<span class="token operator">?</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"E"</span></span><span class="token punctuation">)</span> <span class="token keyword">and</span> name<span class="token punctuation">.</span>end_with<span class="token operator">?</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"h"</span></span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># output</span>
</span><span class="code-line"><span class="token comment"># Earth</span>
</span><span class="code-line">salary <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">399</span><span class="token punctuation">,</span> <span class="token number">234</span><span class="token punctuation">,</span> <span class="token number">566</span><span class="token punctuation">,</span> <span class="token number">533</span><span class="token punctuation">,</span> <span class="token number">233</span><span class="token punctuation">]</span>
</span><span class="code-line">salary<span class="token punctuation">.</span>detect <span class="token punctuation">{</span> <span class="token operator">|</span>s<span class="token operator">|</span> s <span class="token operator">></span> <span class="token number">1000</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># output</span>
</span><span class="code-line"><span class="token comment"># nil</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="find"><a aria-hidden="true" tabindex="-1" href="#find"><span class="icon icon-link"></span></a>find</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">planets <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Mercury"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Venus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Earth"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Mars"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Jupiter"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Saturn"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Uranus"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Neptune"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">planets<span class="token punctuation">.</span>find <span class="token punctuation">{</span> <span class="token operator">|</span>name<span class="token operator">|</span> name<span class="token punctuation">.</span>start_with<span class="token operator">?</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"E"</span></span><span class="token punctuation">)</span> <span class="token keyword">and</span> name<span class="token punctuation">.</span>end_with<span class="token operator">?</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"h"</span></span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># output</span>
</span><span class="code-line"><span class="token comment"># Earth</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="select"><a aria-hidden="true" tabindex="-1" href="#select"><span class="icon icon-link"></span></a>select</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>select <span class="token punctuation">{</span> <span class="token operator">|</span>n<span class="token operator">|</span> n <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 现在你有一个偶数数组</span>
</span><span class="code-line"><span class="token comment"># [2, 4, 6, 8, 10]</span>
</span><span class="code-line"><span class="token comment"># 如果没有满足您的逻辑的值，则返回一个空数组</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>select <span class="token punctuation">{</span> <span class="token operator">|</span>n<span class="token operator">|</span> n <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># no even numbers</span>
</span><span class="code-line"><span class="token comment"># []</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="reject"><a aria-hidden="true" tabindex="-1" href="#reject"><span class="icon icon-link"></span></a>reject</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>reject <span class="token punctuation">{</span> <span class="token operator">|</span>n<span class="token operator">|</span> n <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 如果数字是偶数则拒绝，所以现在我们有一个奇数数组</span>
</span><span class="code-line"><span class="token comment"># [1, 3, 5, 7, 9]</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="keep_if"><a aria-hidden="true" tabindex="-1" href="#keep_if"><span class="icon icon-link"></span></a>keep_if</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>keep_if <span class="token punctuation">{</span> <span class="token operator">|</span>n<span class="token operator">|</span> n <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># numbers 数组仅包含偶数</span>
</span><span class="code-line"><span class="token comment"># [2, 4, 6, 8, 10]</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="delete_if"><a aria-hidden="true" tabindex="-1" href="#delete_if"><span class="icon icon-link"></span></a>delete_if</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>delete_if <span class="token punctuation">{</span> <span class="token operator">|</span>n<span class="token operator">|</span> n <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># numbers 数组仅包含奇数</span>
</span><span class="code-line"><span class="token comment"># [1, 3, 5, 7, 9]</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="drop_while"><a aria-hidden="true" tabindex="-1" href="#drop_while"><span class="icon icon-link"></span></a>drop_while</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span>
</span><span class="code-line">numbers<span class="token punctuation">.</span>drop_while <span class="token punctuation">{</span> <span class="token operator">|</span>n<span class="token operator">|</span> n <span class="token operator">&#x3C;</span> <span class="token number">3</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># 是 3 小于 3，返回 false，所以删除 1, 2</span>
</span><span class="code-line"><span class="token comment"># [3, 1, 2, 3, 0]</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="reverse_each"><a aria-hidden="true" tabindex="-1" href="#reverse_each"><span class="icon icon-link"></span></a>reverse_each</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">words <span class="token operator">=</span> <span class="token string-literal"><span class="token string">%w[first second third fourth fifth sixth]</span></span>
</span><span class="code-line">str <span class="token operator">=</span> <span class="token string-literal"><span class="token string">""</span></span>
</span><span class="code-line">words<span class="token punctuation">.</span>reverse_each <span class="token punctuation">{</span><span class="token operator">|</span>word<span class="token operator">|</span> str <span class="token operator">+=</span> <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">word</span><span class="token delimiter punctuation">}</span></span><span class="token string"> "</span></span><span class="token punctuation">}</span>
</span><span class="code-line">p str <span class="token comment">#=> "sixth fifth fourth third second first "</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="布尔可枚举方法"><a aria-hidden="true" tabindex="-1" href="#布尔可枚举方法"><span class="icon icon-link"></span></a>布尔可枚举方法</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="布尔可枚举方法-1"><a aria-hidden="true" tabindex="-1" href="#布尔可枚举方法-1"><span class="icon icon-link"></span></a>布尔可枚举方法</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->

































<table><thead><tr><th align="left">Name</th><th>When to use</th></tr></thead><tbody><tr><td align="left"><code>all?</code></td><td>当您想检查所有元素是否满足您的条件时</td></tr><tr><td align="left"><code>any?</code></td><td>当您想检查至少一项是否满足您的条件时</td></tr><tr><td align="left"><code>one?</code></td><td>当您想检查一个元素是否满足您的要求时</td></tr><tr><td align="left"><code>none?</code></td><td>当您想检查是否没有任何项目满足您的条件时，相反？</td></tr><tr><td align="left"><code>empty?</code></td><td>当你想检查对象是否为空时</td></tr><tr><td align="left"><code>include?</code></td><td>当你想检查元素是否存在于对象中时</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="all"><a aria-hidden="true" tabindex="-1" href="#all"><span class="icon icon-link"></span></a>all?</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">.</span>all<span class="token operator">?</span> <span class="token punctuation">{</span> <span class="token operator">|</span>num<span class="token operator">|</span> num <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># true</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">.</span>all<span class="token operator">?</span> <span class="token punctuation">{</span> <span class="token operator">|</span>num<span class="token operator">|</span> num <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="any"><a aria-hidden="true" tabindex="-1" href="#any"><span class="icon icon-link"></span></a>any?</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">.</span>any<span class="token operator">?</span> <span class="token punctuation">{</span> <span class="token operator">|</span>num<span class="token operator">|</span> num <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># true</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">19</span><span class="token punctuation">]</span><span class="token punctuation">.</span>any<span class="token operator">?</span> <span class="token punctuation">{</span> <span class="token operator">|</span>num<span class="token operator">|</span> num <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="one"><a aria-hidden="true" tabindex="-1" href="#one"><span class="icon icon-link"></span></a>one?</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">.</span>one<span class="token operator">?</span> <span class="token punctuation">{</span> <span class="token operator">|</span>num<span class="token operator">|</span> num <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># true</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">.</span>one<span class="token operator">?</span> <span class="token punctuation">{</span> <span class="token operator">|</span>num<span class="token operator">|</span> num <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="none"><a aria-hidden="true" tabindex="-1" href="#none"><span class="icon icon-link"></span></a>none?</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">.</span>none<span class="token operator">?</span> <span class="token punctuation">{</span> <span class="token operator">|</span>num<span class="token operator">|</span> num <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># true</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">.</span>none<span class="token operator">?</span> <span class="token punctuation">{</span> <span class="token operator">|</span>num<span class="token operator">|</span> num <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment"># false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="empty"><a aria-hidden="true" tabindex="-1" href="#empty"><span class="icon icon-link"></span></a>empty?</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">.</span>empty<span class="token operator">?</span>
</span><span class="code-line"><span class="token comment"># true</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">.</span>empty<span class="token operator">?</span>
</span><span class="code-line"><span class="token comment"># false</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="组合方法"><a aria-hidden="true" tabindex="-1" href="#组合方法"><span class="icon icon-link"></span></a>组合方法</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="组合方法-1"><a aria-hidden="true" tabindex="-1" href="#组合方法-1"><span class="icon icon-link"></span></a>组合方法</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<ul>
<li><code>&#x26;</code>            返回一个新数组，其中包含在数组和数组 other_array 中找到的每个元素；省略重复；使用 eql? 比较项目</li>
<li><code>intersection</code> 返回一个新数组，其中包含在 self 和所有给定数组 other_arrays 中找到的每个元素；省略重复；使用 eql? 比较项目</li>
<li><code>+</code>            返回一个数组，该数组包含 self 的所有元素，后跟给定数组的所有元素</li>
<li><code>-</code>            返回一个数组，其中包含在给定数组中找不到的所有 self 元素</li>
<li><code>union</code>        返回一个数组，其中包含 self 的所有元素和给定数组的所有元素，已删除重复项</li>
<li><code>difference</code>   返回一个数组，其中包含在任何给定数组中找不到的所有 self 元素</li>
<li><code>product</code>      返回或产生来自 self 和给定数组的所有元素组合</li>
</ul>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id=""><a aria-hidden="true" tabindex="-1" href="#"><span class="icon icon-link"></span></a>&#x26;</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">&#x26;</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span> <span class="token comment"># => [1, 2]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">&#x26;</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token comment"># => [0, 1]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="intersection"><a aria-hidden="true" tabindex="-1" href="#intersection"><span class="icon icon-link"></span></a>intersection</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line"><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span>intersection<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># => [0, 1]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span>intersection<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># => [0, 1]</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="-1"><a aria-hidden="true" tabindex="-1" href="#-1"><span class="icon icon-link"></span></a>+</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>
</span><span class="code-line">a <span class="token comment"># => [0, 1, 2, 3]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="-"><a aria-hidden="true" tabindex="-1" href="#-"><span class="icon icon-link"></span></a>-</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> 
</span><span class="code-line"><span class="token comment"># => [0, 2, 3]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span> 
</span><span class="code-line"><span class="token comment"># => [1, 2]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">-</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span> 
</span><span class="code-line"><span class="token comment"># => [0, 1, 2]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="union"><a aria-hidden="true" tabindex="-1" href="#union"><span class="icon icon-link"></span></a>union</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span>union<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token comment"># => [0, 1, 2, 3, 4, 5, 6, 7]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>union<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token comment"># => [0, 1, 2, 3]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span>union<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token comment"># => [0, 1, 2, 3]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="difference"><a aria-hidden="true" tabindex="-1" href="#difference"><span class="icon icon-link"></span></a>difference</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>difference<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># => [0, 2, 3]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span>difference<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># => [2]</span>
</span><span class="code-line"><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span>difference<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment"># => [0, 1, 2]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="product"><a aria-hidden="true" tabindex="-1" href="#product"><span class="icon icon-link"></span></a>product</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-ruby code-highlight"><span class="code-line">a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span>
</span><span class="code-line">a1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span>
</span><span class="code-line">p <span class="token operator">=</span> a<span class="token punctuation">.</span>product<span class="token punctuation">(</span>a1<span class="token punctuation">)</span>
</span><span class="code-line">p<span class="token punctuation">.</span>size <span class="token comment"># => 6 # a.size * a1.size</span>
</span><span class="code-line">p <span class="token comment"># => [[0, 3], [0, 4], [1, 3], [1, 4], [2, 3], [2, 4]]</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="循环"><a aria-hidden="true" tabindex="-1" href="#循环"><span class="icon icon-link"></span></a>循环</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="while-循环"><a aria-hidden="true" tabindex="-1" href="#while-循环"><span class="icon icon-link"></span></a>while 循环</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token comment"># variable count</span>
</span><span class="code-line">count <span class="token operator">=</span> <span class="token number">4</span>
</span><span class="code-line"><span class="token comment"># using while loop</span>
</span><span class="code-line"><span class="token comment"># here conditional is count i.e. 4</span>
</span><span class="code-line"><span class="token keyword">while</span> count <span class="token operator">>=</span> <span class="token number">1</span>
</span><span class="code-line">  <span class="token comment"># statements to be executed</span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"Ruby Cheatsheet"</span></span>
</span><span class="code-line">  count <span class="token operator">=</span> count <span class="token operator">-</span> <span class="token number">1</span>
</span><span class="code-line">  <span class="token comment"># while loop ends here</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
<p>输出</p>
<pre><code class="code-highlight"><span class="code-line">Ruby Cheatsheet
</span><span class="code-line">Ruby Cheatsheet
</span><span class="code-line">Ruby Cheatsheet
</span><span class="code-line">Ruby Cheatsheet
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="for-循环"><a aria-hidden="true" tabindex="-1" href="#for-循环"><span class="icon icon-link"></span></a>for 循环</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token comment"># loop using range as expression</span>
</span><span class="code-line">text <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Ruby Cheatsheet"</span></span>
</span><span class="code-line"><span class="token comment"># using for loop with the range</span>
</span><span class="code-line"><span class="token keyword">for</span> count <span class="token keyword">in</span> <span class="token number">1.</span><span class="token number">.5</span> <span class="token keyword">do</span>
</span><span class="code-line">  puts text
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
<p>输出</p>
<pre><code class="code-highlight"><span class="code-line">Ruby Cheatsheet
</span><span class="code-line">Ruby Cheatsheet
</span><span class="code-line">Ruby Cheatsheet
</span><span class="code-line">Ruby Cheatsheet
</span><span class="code-line">Ruby Cheatsheet
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="dowhile-循环"><a aria-hidden="true" tabindex="-1" href="#dowhile-循环"><span class="icon icon-link"></span></a>do..while 循环</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token comment"># starting of do..while loop</span>
</span><span class="code-line">loop <span class="token keyword">do</span>
</span><span class="code-line">  puts <span class="token string-literal"><span class="token string">"Ruby Cheatsheet"</span></span>
</span><span class="code-line">  val <span class="token operator">=</span> <span class="token string-literal"><span class="token string">'7'</span></span>
</span><span class="code-line">  <span class="token comment"># using boolean expressions</span>
</span><span class="code-line">  <span class="token keyword">if</span> val <span class="token operator">==</span> <span class="token string-literal"><span class="token string">'7'</span></span>
</span><span class="code-line">    <span class="token keyword">break</span>
</span><span class="code-line">  <span class="token keyword">end</span>
</span><span class="code-line">  <span class="token comment"># ending of ruby do..while loop</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
<p>输出</p>
<pre><code class="code-highlight"><span class="code-line">Ruby Cheatsheet
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="until-循环"><a aria-hidden="true" tabindex="-1" href="#until-循环"><span class="icon icon-link"></span></a>until 循环</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">var <span class="token operator">=</span> <span class="token number">7</span>
</span><span class="code-line"><span class="token comment"># here do is optional</span>
</span><span class="code-line"><span class="token keyword">until</span> var <span class="token operator">==</span> <span class="token number">11</span> <span class="token keyword">do</span>
</span><span class="code-line">  <span class="token comment"># code to be executed</span>
</span><span class="code-line">  puts var <span class="token operator">*</span> <span class="token number">10</span>
</span><span class="code-line">  var <span class="token operator">=</span> var <span class="token operator">+</span> <span class="token number">1</span>
</span><span class="code-line">  <span class="token comment"># here loop ends</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
<p>输出</p>
<pre><code class="code-highlight"><span class="code-line">70
</span><span class="code-line">80
</span><span class="code-line">90
</span><span class="code-line">100
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="跳出循环"><a aria-hidden="true" tabindex="-1" href="#跳出循环"><span class="icon icon-link"></span></a>跳出循环</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">salary <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">399</span><span class="token punctuation">,</span> <span class="token number">234</span><span class="token punctuation">,</span> <span class="token number">566</span><span class="token punctuation">,</span> <span class="token number">533</span><span class="token punctuation">,</span> <span class="token number">233</span><span class="token punctuation">]</span>
</span><span class="code-line">salary<span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token keyword">do</span> <span class="token operator">|</span>s<span class="token operator">|</span>
</span><span class="code-line">  <span class="token keyword">break</span> <span class="token keyword">if</span> s <span class="token operator">==</span> <span class="token number">566</span>
</span><span class="code-line">  puts s
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 399</span>
</span><span class="code-line"><span class="token comment"># 234</span>
</span></code></pre>
<p>通过使用 <code>break</code> 关键字</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="在循环内跳过"><a aria-hidden="true" tabindex="-1" href="#在循环内跳过"><span class="icon icon-link"></span></a>在循环内跳过</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">salary <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">399</span><span class="token punctuation">,</span> <span class="token number">234</span><span class="token punctuation">,</span> <span class="token number">566</span><span class="token punctuation">,</span> <span class="token number">533</span><span class="token punctuation">,</span> <span class="token number">233</span><span class="token punctuation">]</span>
</span><span class="code-line">salary<span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token keyword">do</span> <span class="token operator">|</span>s<span class="token operator">|</span>
</span><span class="code-line">  <span class="token keyword">next</span> <span class="token keyword">if</span> s <span class="token operator">==</span> <span class="token number">533</span>
</span><span class="code-line">  puts s
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line"><span class="token comment"># 输出</span>
</span><span class="code-line"><span class="token comment"># 399</span>
</span><span class="code-line"><span class="token comment"># 234</span>
</span><span class="code-line"><span class="token comment"># 566</span>
</span><span class="code-line"><span class="token comment"># 233</span>
</span></code></pre>
<p>通过使用 next 关键字</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="重复当前迭代"><a aria-hidden="true" tabindex="-1" href="#重复当前迭代"><span class="icon icon-link"></span></a>重复当前迭代</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">data <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">456</span><span class="token punctuation">,</span> <span class="token number">3000</span><span class="token punctuation">]</span>
</span><span class="code-line">retry_count <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line">status <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"network failure"</span></span>
</span><span class="code-line">sum <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line">data<span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token keyword">do</span> <span class="token operator">|</span>d<span class="token operator">|</span>
</span><span class="code-line">    <span class="token keyword">if</span> retry_count <span class="token operator">==</span> <span class="token number">3</span>
</span><span class="code-line">        status <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"connection established"</span></span>
</span><span class="code-line">        retry_count <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line">        <span class="token keyword">redo</span>
</span><span class="code-line">    <span class="token keyword">elsif</span> status <span class="token operator">==</span> <span class="token string-literal"><span class="token string">"network failure"</span></span> <span class="token keyword">and</span> retry_count <span class="token operator">&#x3C;</span> <span class="token number">5</span>
</span><span class="code-line">        puts <span class="token string-literal"><span class="token string">"network failure </span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">retry_count</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line">        retry_count <span class="token operator">+=</span> <span class="token number">1</span>
</span><span class="code-line">        <span class="token keyword">redo</span>
</span><span class="code-line">    <span class="token keyword">elsif</span> status <span class="token operator">==</span> <span class="token string-literal"><span class="token string">"connection established"</span></span>
</span><span class="code-line">        puts d
</span><span class="code-line">        sum <span class="token operator">+=</span> d
</span><span class="code-line">    <span class="token keyword">end</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line"><span class="token comment"># output of sum</span>
</span><span class="code-line"><span class="token comment"># 3456</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="重新开始循环"><a aria-hidden="true" tabindex="-1" href="#重新开始循环"><span class="icon icon-link"></span></a>重新开始循环</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">numbers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">44</span><span class="token punctuation">,</span> <span class="token number">44</span><span class="token punctuation">]</span>
</span><span class="code-line">sum <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line"><span class="token keyword">begin</span>
</span><span class="code-line">    numbers<span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token keyword">do</span> <span class="token operator">|</span>s<span class="token operator">|</span>
</span><span class="code-line">        <span class="token keyword">if</span> rand<span class="token punctuation">(</span><span class="token number">1.</span><span class="token number">.10</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">5</span>
</span><span class="code-line">            puts <span class="token string-literal"><span class="token string">"hi 5, let's do it again!"</span></span>
</span><span class="code-line">            sum <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line">            <span class="token keyword">raise</span> <span class="token string-literal"><span class="token string">"hi 5"</span></span>
</span><span class="code-line">        <span class="token keyword">end</span>
</span><span class="code-line">        puts s
</span><span class="code-line">        sum <span class="token operator">+=</span> s
</span><span class="code-line">    <span class="token keyword">end</span>
</span><span class="code-line"><span class="token keyword">rescue</span>
</span><span class="code-line">    <span class="token keyword">retry</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="classes"><a aria-hidden="true" tabindex="-1" href="#classes"><span class="icon icon-link"></span></a>Classes</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="classes-示例"><a aria-hidden="true" tabindex="-1" href="#classes-示例"><span class="icon icon-link"></span></a>Classes 示例</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Person</span>
</span><span class="code-line">    <span class="token comment"># when you create a new object, it looks for a method named initialize and executes it, like a constructor in java</span>
</span><span class="code-line">    <span class="token comment"># def initialize(name, number)</span>
</span><span class="code-line">    <span class="token comment">#    @name = name</span>
</span><span class="code-line">    <span class="token comment">#    @number = number</span>
</span><span class="code-line">    <span class="token comment"># end</span>
</span><span class="code-line">    <span class="token comment"># instance variable</span>
</span><span class="code-line">    <span class="token comment"># @name</span>
</span><span class="code-line">    <span class="token comment"># class variable</span>
</span><span class="code-line">    <span class="token comment"># @@count</span>
</span><span class="code-line">    <span class="token comment"># attr_accessor acts as a getter and setter for the following instance attributes</span>
</span><span class="code-line">    attr_accessor <span class="token symbol">:name</span><span class="token punctuation">,</span> <span class="token symbol">:number</span>
</span><span class="code-line">    <span class="token comment"># class variable must be initialized</span>
</span><span class="code-line">    <span class="token variable">@@count</span> <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token method-definition"><span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">count</span></span>
</span><span class="code-line">        <span class="token variable">@@count</span>
</span><span class="code-line">    <span class="token keyword">end</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token method-definition"><span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">count</span></span><span class="token operator">=</span><span class="token punctuation">(</span>count<span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token variable">@@count</span> <span class="token operator">=</span> count
</span><span class="code-line">    <span class="token keyword">end</span>
</span><span class="code-line">    <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">initialize</span></span>
</span><span class="code-line">        <span class="token variable">@@count</span> <span class="token operator">+=</span> <span class="token number">1</span>
</span><span class="code-line">    <span class="token keyword">end</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line"><span class="token comment"># create an instance of the Person class</span>
</span><span class="code-line">p1 <span class="token operator">=</span> <span class="token class-name">Person</span><span class="token punctuation">.</span><span class="token keyword">new</span>
</span><span class="code-line"><span class="token comment"># set attributes of the Person class</span>
</span><span class="code-line">p1<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Yukihiro Matsumoto"</span></span>
</span><span class="code-line">p1<span class="token punctuation">.</span>number <span class="token operator">=</span> <span class="token number">9999999999</span>
</span><span class="code-line"><span class="token comment"># get attributes of the Person class</span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">p1<span class="token punctuation">.</span>name</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">p1<span class="token punctuation">.</span>number</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">Person<span class="token punctuation">.</span>count</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line"><span class="token comment"># Yukihiro Matsumoto</span>
</span><span class="code-line"><span class="token comment"># 9999999999</span>
</span><span class="code-line"><span class="token comment"># 1</span>
</span><span class="code-line">p2 <span class="token operator">=</span> <span class="token class-name">Person</span><span class="token punctuation">.</span><span class="token keyword">new</span>
</span><span class="code-line">p2<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Yukihiro Matsumoto"</span></span>
</span><span class="code-line">p2<span class="token punctuation">.</span>number <span class="token operator">=</span> <span class="token number">9999999999</span>
</span><span class="code-line"><span class="token comment"># get attributes of the Person class</span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">p2<span class="token punctuation">.</span>name</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">p2<span class="token punctuation">.</span>number</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">Person<span class="token punctuation">.</span>count</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line"><span class="token comment"># Yukihiro Matsumoto</span>
</span><span class="code-line"><span class="token comment"># 9999999999</span>
</span><span class="code-line"><span class="token comment"># 2</span>
</span><span class="code-line"><span class="token comment"># set class variable</span>
</span><span class="code-line">Person<span class="token punctuation">.</span>count <span class="token operator">=</span> <span class="token number">3</span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">Person<span class="token punctuation">.</span>count</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line"><span class="token comment"># 3</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="继承一个类"><a aria-hidden="true" tabindex="-1" href="#继承一个类"><span class="icon icon-link"></span></a>继承一个类</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Person</span>
</span><span class="code-line">    attr_accessor <span class="token symbol">:name</span><span class="token punctuation">,</span> <span class="token symbol">:number</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line"><span class="token comment"># 使用 &#x3C; 符号从父类继承方法和属性</span>
</span><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token operator">&#x3C;</span> Person
</span><span class="code-line">    attr_accessor <span class="token symbol">:id</span>
</span><span class="code-line"><span class="token keyword">end</span>
</span><span class="code-line">s <span class="token operator">=</span> <span class="token class-name">Student</span><span class="token punctuation">.</span><span class="token keyword">new</span>
</span><span class="code-line">s<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"James Bond"</span></span>
</span><span class="code-line">s<span class="token punctuation">.</span>number <span class="token operator">=</span> <span class="token number">700</span>
</span><span class="code-line">s<span class="token punctuation">.</span>id <span class="token operator">=</span> <span class="token number">678</span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">p<span class="token punctuation">.</span>name</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line">James Bond
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">p<span class="token punctuation">.</span>number</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line"><span class="token number">700</span>
</span><span class="code-line">puts <span class="token string-literal"><span class="token string">"</span><span class="token interpolation"><span class="token delimiter punctuation">#{</span><span class="token content">p<span class="token punctuation">.</span>id</span><span class="token delimiter punctuation">}</span></span><span class="token string">"</span></span>
</span><span class="code-line"><span class="token number">678</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="检查实例类型"><a aria-hidden="true" tabindex="-1" href="#检查实例类型"><span class="icon icon-link"></span></a>检查实例类型</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Vehicle</span><span class="token punctuation">;</span> <span class="token keyword">end</span>
</span><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Car</span> <span class="token operator">&#x3C;</span> Vehicle<span class="token punctuation">;</span> <span class="token keyword">end</span>
</span><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Audi</span> <span class="token operator">&#x3C;</span> Car<span class="token punctuation">;</span> <span class="token keyword">end</span>
</span><span class="code-line">car <span class="token operator">=</span> <span class="token class-name">Car</span><span class="token punctuation">.</span><span class="token keyword">new</span>
</span><span class="code-line">car<span class="token punctuation">.</span>instance_of<span class="token operator">?</span> Vehicle
</span><span class="code-line"><span class="token boolean">false</span>
</span><span class="code-line">car<span class="token punctuation">.</span>instance_of<span class="token operator">?</span> Car
</span><span class="code-line"><span class="token boolean">true</span>
</span><span class="code-line">car<span class="token punctuation">.</span>instance_of<span class="token operator">?</span> Audi
</span><span class="code-line"><span class="token boolean">false</span>
</span><span class="code-line">a <span class="token operator">=</span> <span class="token number">7</span>
</span><span class="code-line">a<span class="token punctuation">.</span>instance_of<span class="token operator">?</span> <span class="token builtin">Integer</span>
</span><span class="code-line"><span class="token boolean">true</span>
</span><span class="code-line">a<span class="token punctuation">.</span>instance_of<span class="token operator">?</span> <span class="token builtin">Numeric</span>
</span><span class="code-line"><span class="token boolean">false</span>
</span></code></pre>
<p>如果对象是给定类的实例，而不是子类或超类，则返回 true</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="打印一个类的所有方法名"><a aria-hidden="true" tabindex="-1" href="#打印一个类的所有方法名"><span class="icon icon-link"></span></a>打印一个类的所有方法名</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line">puts <span class="token punctuation">(</span><span class="token builtin">String</span><span class="token punctuation">.</span>methods<span class="token punctuation">)</span><span class="token punctuation">.</span>sort
</span><span class="code-line"><span class="token comment"># 排除从 Object 类继承的方法</span>
</span><span class="code-line">puts <span class="token punctuation">(</span><span class="token builtin">String</span><span class="token punctuation">.</span>methods <span class="token operator">-</span> <span class="token builtin">Object</span><span class="token punctuation">.</span>public_instance_methods<span class="token punctuation">)</span><span class="token punctuation">.</span>sort
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="检查一个类是否有特定的方法"><a aria-hidden="true" tabindex="-1" href="#检查一个类是否有特定的方法"><span class="icon icon-link"></span></a>检查一个类是否有特定的方法</h3><div class="wrap-body">
<pre class="language-ruby"><code class="language-ruby code-highlight"><span class="code-line"><span class="token builtin">String</span><span class="token punctuation">.</span>respond_to<span class="token operator">?</span><span class="token punctuation">(</span><span class="token symbol">:prepend</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token boolean">true</span>
</span><span class="code-line"><span class="token builtin">String</span><span class="token punctuation">.</span>respond_to<span class="token operator">?</span><span class="token punctuation">(</span><span class="token symbol">:append</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token boolean">false</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="https://www.ruby-lang.org/en/">Ruby 官网</a> <em>(ruby-lang.org)</em></li>
<li><a href="https://github.com/lifeparticle/Ruby-Cheatsheet">Ruby Cheatsheet</a> <em>(github.com)</em></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 Kenny Wang.</footer></footer><script src="..\/data.js?v=1.4.1" defer></script><script src="..\/js/fuse.min.js?v=1.4.1" defer></script><script src="..\/js/main.js?v=1.4.1" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
